Kotlin 中文文档 v2.0.10 Help

This 表达式

要表示当前 接收者 ,你使用 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. 部分。如果有一个同名的非成员函数,请谨慎使用,因为在某些情况下它可能会被调用:

fun main() { //sampleStart fun printLine() { println("顶级函数") } class A { fun printLine() { println("成员函数") } fun invokePrintLine(omitThis: Boolean = false) { if (omitThis) printLine() else this.printLine() } } A().invokePrintLine() // 成员函数 A().invokePrintLine(omitThis = true) // 顶级函数 //sampleEnd() }
Last modified: 08 九月 2024