별의 공부 블로그 🧑🏻‍💻
728x90
728x170

이너 클래스 에서 바깥 클래스의 상위 클래스 를 호출하려는 경우

  • super 키워드와 함께 @ 기호 옆에 바깥 클래스 이름을 작성함.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
open class Base {
    open val x: Int = 1
    open fun f() = println("Base Class f()")
}

class Child : Base() {
    override val x: Int = super.x + 1
    override fun f() = println("Child Class f())")

    // 이너 클래스
    inner class Inside {
        fun f() = println("Inside Class f()")
        fun test() {
            f()                 // (1) 현재 이너 클래스의 f() 접근
            Child().f()         // (2) 바로 바깥 클래스 f() 접근
            super@Child.f()     // (3) Child의 상위 클래스인 Base 클래스의 f() 접근
            println("[Inside] super@Child.x: ${super@Child.x}")     // (4) Base의 x 접근
        }
    }
}

fun main() {
    val c1 = Child()
    c1.Inside().test()      // 이너 클래스 Inside의 메서드 test() 실행
}

 

 

Inside Class f()
Child Class f())
Base Class f()
[Inside] super@Child.x: 1
728x90
그리드형(광고전용)
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️
starrykss
starrykss
별의 공부 블로그 🧑🏻‍💻


📖 Contents 📖