我想在创建“匿名内部类”时访问调用类的范围
在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
}
}
}
}
答案 0 :(得分:48)
this@MyClass
JFYI: 访问扩展函数接收器的语法相同:
fun MyClass.foo() {
// in some nested thing:
this@foo
//...
}
Kotlin参考:This expressions
答案 1 :(得分:3)
在我的情况下,我访问它:/**
* @var Notification[]|ArrayCollection
*/
public function getAllNotifications()
{
return new ArrayCollection(
array_merge(
$this->sentNotifications->toArray(),
$this->receivedNotifications->toArray()
)
);
}
this@MainActivity