Kotlin 其他(五) --- This 表达式
来源:互联网 发布:c语言内容有多少 编辑:程序博客网 时间:2024/06/05 13:30
This 表达式
为了表示当前的 接收者 我们使用 this
表达式:
- 在类的成员中,
this
指的是该类的当前对象。 - 在扩展函数或者带接收者的函数字面值中, this 表示在点左侧传递的 接收者 参数。
如果 this
没有限定符,它指的是最内层的包含它的作用域。要引用其他作用域中的 this
,请使用 标签限定符:
2、限定的 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 的接收者 } val funLit2 = { s: String -> // foo() 的接收者,因为它包含的 lambda 表达式 // 没有任何接收者 val d1 = this } } }}
阅读全文
0 0
- Kotlin 其他(五) --- This 表达式
- Kotlin-29.this表达式(this Expression)
- Kotlin学习(九): 扩展(Extensions)和This表达式
- Kotlin语法(其他)
- Kotlin语法(五)
- Kotlin-其他
- Kotlin-this
- kotlin学习笔记(五)
- Kotlin基础(五)类
- Kotlin学习记录(五)—— 条件表达式的使用
- Kotlin 其他(二) --- 集合(Collections)
- Kotlin 其他(三) --- 区间(Ranges)
- Kotlin 其他(九) --- 异常(Exceptions)
- Kotlin(五)
- Kotlin for Android(五)Kotlin类和对象
- Kotlin语法(五)-返回和跳转
- Kotlin笔记(五)——继承
- Kotlin类和对象 (五)--- 扩展
- 【POJ
- 进制转换(十转二)
- json解析
- 如何配置webpack(学习总结)
- 跨域资源共享CORS以及在zepto中使用遇到的问题
- Kotlin 其他(五) --- This 表达式
- 初识Android AIDL重要点记录(Android Studio)
- golang time.Format坑
- AndroidStudio getColor方法过时
- erlang 数据结构 --maps
- MQTT--mosquitto用户名密码配置
- selector socketChannel
- VS2017 C++ 无法打开源文件: “SDKDDKVer.h”, "stdio.h", "tchar.h"
- 【web前端】HTML5抽奖转盘demo学习笔记