Kotlin外部范围
在Kotlin中创建“匿名内部类”时,我想访问调用类的范围。 Java的OuterScope.this
语法相当于什么? 例如:
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 } } } }
this@MyClass
JFYI:与扩展函数的接收者访问相同的语法:
fun MyClass.foo() { // in some nested thing: this@foo //... }
Kotlin参考: 这个表达式