상세 컨텐츠

본문 제목

[Kotlin] 중첩 클래스

프로그래밍

by 독서와 여행 2021. 4. 6. 20:15

본문

 

package com.example.complie
typealias Username = String




fun main() {
    val output = Outer.Nested.grer() //내부 클래스는 객체 생성 없이 사용 가능

    //Outer.outside() <- 외부 클래스는 객체 생성해야함
}

inline fun inlineLambda(a: Int, b: Int, out:(Int, Int) -> Unit){
    out(a,b)
}

class Outer{

    val ov = 5

    class Nested{
        val nv = 10 // out 변수인 ov는 불가
        fun grer() = println("Hello")
        som() // companion object는 사용가능
    }

    companion object{
        const val country = "Korea"
        fun som = println("Simple")
    }

    fun outside() = Nested().grer() //객체 생성 없이 중첩 클래스의 매서드 접근
    val test:Int = Nested().nv // 객체 생성 없이 중첩 클래스 프로퍼티 접근 

}

관련글 더보기

댓글 영역