即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

Kotlin初步入门基础知识教程-this关键字

编程语言 qhshiniba 11℃ 0评论
本文目录
[隐藏]

this

1.this

this关键字可以作用到类,函数,lambda表达式


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 {String.() ->
        val d = this // funLit's receiver
        val d1 = this@lambda // funLit's receiver
      }


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

转载请注明:CodingBlog » Kotlin初步入门基础知识教程-this关键字

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情