Kotlin-29.this表达式(this Expression)
来源:互联网 发布:微信网络用语大全 编辑:程序博客网 时间:2024/06/03 06:40
官方文档: http://kotlinlang.org/docs/reference/this-expressions.html
1.this表达式
在kotlin中,可用this表达式表示当前接收者(receiver)对象 1.在类成员函数中,this代指该类的当前对象; 2.在扩展函数(extension function) 或者带接收者的字面函数(function literal with receiver)中, this代指该函数的接收者对象参数(receiver parameter);如果this没有限定符(qualifiers),代指包含它的最内层作用域的对象;如果要使用外部作用域的this,就要添加this标签限定符(label qualifiers)
2.this限定符(Qualifier)-this@label
访问来自外部作用域的this(类,扩展函数,带接收者的字面函数)我们使用this@label,其中 @label 是一个代指 this 来源的标签:fun main(args: Array<String>) { A().B().p()}class A { //隐式标签 @A inner class B { //隐式标签 @B fun p(){ println(this)//输出A$B@279f2327,this代指[B类对象] 666.foo() } fun Int.foo() { //隐式标签 @foo //输出A@2ff4acd0, this代指[A类对象] println(this@A) //输出A$B@279f2327, this代指[B类对象] println(this@B) //输出666, this代指[foo函数接收者Int类对象] println(this) //输出666, this@foo代指[foo函数接收者Int类对象] println(this@foo) val funLit = fun String.() { //this代指[funLit函数接收者String类对象] println(this) //输出lit } "lit".funLit() val funLit2 = { s: String -> //该函数没有接收者,故this代指[foo函数接收者Int类对象] println(this) //输出666 } funLit2("lit2") } }}
简书:http://www.jianshu.com/p/8112eea496cf
CSDN博客: http://blog.csdn.net/qq_32115439/article/details/74276017
GitHub博客:http://lioil.win/2017/07/03/Kotlin-this.html
Coding博客:http://c.lioil.win/2017/07/03/Kotlin-this.html
阅读全文
0 0
- Kotlin-29.this表达式(this Expression)
- Kotlin-this
- Kotlin 其他(五) --- This 表达式
- Kotlin基础教程-this关键字
- Kotlin学习(九): 扩展(Extensions)和This表达式
- blr:expression(this.onFocus=this.blur())
- This is an expression test.
- width: expression(this.width > 50
- 学习kotlin第14天_区间、类型检查与转换、this表达式、相等性
- this
- this
- this
- this
- this
- this
- this
- this
- this
- 10、Oracle:左连接、右连接、全外连接以及(+)号用法
- 关于canvas的重要方法用法总结
- 简单选择排序和冒泡排序
- MySQL安装
- 【动态规划22】LiberOJ#515. 「LibreOJ β Round #2」贪心只能过样例(bitset优化)
- Kotlin-29.this表达式(this Expression)
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- 选自TED演讲《做一个更好的交谈者》
- 堆和栈的区别(直接转)
- 程序员之路-学习经验总结分享
- Linux杂记
- 女性婚前性行为的十个误区
- ssh client 报 algorithm negotiation failed的解决方法
- vue中的webpack