跳到主要内容

This 表达式

要表示当前的 接收者 (receiver),可以使用 this 表达式:

如果 this 没有限定符,则它指的是 最内层的封闭作用域。 要引用其他作用域中的 this,可以使用 标签限定符

限定的 this

要从外部作用域(扩展函数 或带标签的 带接收者的函数字面量)访问 this,可以编写 this@label,其中 @label 是作用域 标签,表示 this 应该来自哪里:

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() 的接收者,一个 Int
val c1 = this@foo // foo() 的接收者,一个 Int

val funLit = lambda@ fun String.() {
val d = this // funLit 的接收者,一个 String
}

val funLit2 = { s: String `->`
// foo() 的接收者,因为封闭的 lambda 表达式
// 没有接收者
val d1 = this
}
}
}
}

隐式的 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
}