此表示式
要表示目前的 接收者 (receiver),可以使用 this
表達式:
如果 this
沒有限定詞,則它指的是 最內層的封閉作用域 (innermost enclosing scope)。 要在其他作用域中引用 this
,可以使用 標籤限定詞 (label qualifiers):
限定的 this (Qualified this)
要從外部作用域(類別、擴充函式 或標記的帶接收者的函數文本)訪問 this
,可以寫入 this@label
,其中 @label
是作用域上的一個 標籤,表示 this
應該來自哪裡:
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, a String
}
val funLit2 = { s: String `->`
// foo()'s receiver, since enclosing lambda expression
// doesn't have any receiver
val d1 = this
}
}
}
}
隱式的 this (Implicit this)
當你在 this
上呼叫成員函式時,你可以省略 this.
部分。
如果你有一個具有相同名稱的非成員函式,請謹慎使用 this
,因為在某些情況下可能會呼叫它:
fun main() {
fun printLine() { println("Local function") }
class A {
fun printLine() { println("Member function") }
fun invokePrintLine(omitThis: Boolean = false) {
if (omitThis) printLine()
else this.printLine()
}
}
A().invokePrintLine() // Member function
A().invokePrintLine(omitThis = true) // Local function
}