為了表示當(dāng)前的 接收者 我們使用 this{: .keyword } 表達式:
如果 this{: .keyword } 沒有限定符,它指的是最內(nèi)層的包含它的作用域。要引用其他作用域中的 this{: .keyword },請使用 標(biāo)簽限定符:
要訪問來自外部作用域的this{: .keyword }(一個類 或者擴展函數(shù),
或者帶標(biāo)簽的帶接收者的函數(shù)字面值)我們使用this@label,其中 @label 是一個
代指 this{: .keyword } 來源的標(biāo)簽:
class A { // 隱式標(biāo)簽 @A
inner class B { // 隱式標(biāo)簽 @B
fun Int.foo() { // 隱式標(biāo)簽 @foo
val a = this@A // A 的 this
val b = this@B // B 的 this
val c = this // foo() 的接收者,一個 Int
val c1 = this@foo // foo() 的接收者,一個 Int
val funLit = lambda@ fun String.() {
val d = this // funLit 的接收者
}
val funLit2 = { s: String ->
// foo() 的接收者,因為它包含的 lambda 表達式
// 沒有任何接收者
val d1 = this
}
}
}
}