Kotlin Reference: This Expression

  使用 this 表达式表示当前的接收者(Receiver):

  如果 this 不具有限定词,则它指的是自身所在的最内层的作用域。如果要引用外层作用域,则需要使用标签限定符(Label Qualifier):

Qualified this

  如果想要访问外层作用域(如扩展函数或有标签的带接收者的函数字面值)的 this,需要使用 this@label,其中 @labelthis 来源作用域的标签

class A { // implicit label @A
inner class B { // implicit label @B
fun Int.foo() { // implicit label @foo
val a = this@A // A's this
val b = this@B // B's this

val c = this // foo()'s receiver, an Int
val c1 = this@foo // foo()'s receiver, an Int

val funLit = lambda@ fun String.() {
val d = this // funLit's receiver
}

val funLit2 = { s: String ->
// foo()'s receiver, since enclosing lambda expression
// doesn't have any receiver
val d1 = this
}
}
}
}