this 表達式
為了表示目前的 receiver,您可以使用 this 表達式:
- 在 類別 的成員中,
this指的是該類別的目前物件。 - 在 擴充方法 或 帶有接收者的函式常值 中,
this表示在點(dot)左側傳遞的 receiver 參數。
如果 this 沒有限定詞,它指的是 最內層的封閉作用域。若要參考其他作用域中的 this,則使用 標籤限定詞:
限定的 this
若要從外層作用域(類別、擴充方法 或具標籤的 帶有接收者的函式常值)存取 this,您可以撰寫 this@label,其中 @label 是 this 所屬作用域上的 標籤:
kotlin
class A { // 隱式標籤 @A
inner class B { // 隱式標籤 @B
fun Int.foo() { // 隱式標籤 @foo
val a = this@A // A 的 this
val b = this@B // B 的 this
val c = this // foo() 的 receiver,一個 Int
val c1 = this@foo // foo() 的 receiver,一個 Int
val funLit = lambda@ fun String.() {
val d = this // funLit 的 receiver,一個 String
}
val funLit2 = { s: String ->
// foo() 的 receiver,因為封閉的 Lambda 運算式
// 沒有任何 receiver
val d1 = this
}
}
}
}隱式 this
當您在 this 上呼叫成員函數時,可以省略 this. 限定詞。然而,如果另一個具有相同名稱的可呼叫物件在更近的語法作用域內可用,Kotlin 會將不帶限定詞的呼叫解析為該物件,而非成員函數。若要明確呼叫成員函數,請使用 this. 限定詞:
kotlin
fun main() {
class A {
fun printLine() {
println("Member function")
}
fun invokePrintLine() {
fun printLine() {
println("Local function")
}
printLine()
// Local function
this.printLine()
// Member function
}
}
A().invokePrintLine()
}