Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

Kotlin outer scope

I would like to access the scope of the calling class when creating an "anonymous inner class" in Kotlin. What would be the equivalent of Java's OuterScope.this syntax? example :

open class SomeClass {
    open fun doSomething() {
        // ...
    }
}

class MyClass {
    fun someFunc() {
        object : SomeClass() {
            override fun doSomething() {
                super<SomeClass>.doSomething()
                // Access the outer class context, in Java
                // this would be MyClass.this
            }
        }
    }
}
question from:https://stackoverflow.com/questions/26503640/kotlin-outer-scope

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
this@MyClass

JFYI: the same syntax for access to receiver of extension function:

fun MyClass.foo() {
    // in some nested thing:
    this@foo
    //...
}

Kotlin Reference: This expressions


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...