限定this

为了在其他范围(类或扩展函数、函数文本标签)使用this,我们使用this@label,@label是一个范围内的标签:

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
            }

            val funLit2 = { s: String ->
                // foo()'s receiver, since enclosing lambda expression
                // doesn't have any receiver
                val d1 = this
            }
        }
    }
}

results matching ""

    No results matching ""