Kotlin之let,apply,with,run函数区别
来源:互联网 发布:js 多图上传插件 编辑:程序博客网 时间:2024/05/13 01:26
Kotlin之let,apply,with,run函数区别
很长一段时间内都一直使用Kotlin这门语言,也只是纯粹使用简单语法,最近有时候写的代码,编辑器自动提示使用let等函数,然后就专门花点时间研究了下。
let
首先let()的定义是这样的,默认当前这个对象作为闭包的it参数,返回值是函数里面最后一行,或者指定return
fun <T, R> T.let(f: (T) -> R): R = f(this)
简单示例:
fun testLet(): Int { // fun <T, R> T.let(f: (T) -> R): R { f(this)} "testLet".let { println(it) println(it) println(it) return 1 }}//运行结果//testLet//testLet//testLet
可以看看最后生成的class文件,代码已经经过格式化了,编译器只是在我们原先的变量后面添加了let里面的内容。
public static final int testLet() { String str1 = "testLet"; String it = (String)str1; int $i$a$1$let; System.out.println(it); System.out.println(it); System.out.println(it); return 1;}
来个复杂一定的例子
fun testLet(): Int { // fun <T, R> T.let(f: (T) -> R): R { f(this)} "testLet".let { if (Random().nextBoolean()) { println(it) return 1 } else { println(it) return 2 } }}
编译过后的class文件
public static final int testLet() { String str1 = "testLet"; String it = (String)str1; int $i$a$1$let; if (new Random().nextBoolean()) { System.out.println(it); return 1; } System.out.println(it); return 2;}
apply
apply函数是这样的,调用某对象的apply函数,在函数范围内,可以任意调用该对象的任意方法,并返回该对象
fun <T> T.apply(f: T.() -> Unit): T { f(); return this }
代码示例
fun testApply() { // fun <T> T.apply(f: T.() -> Unit): T { f(); return this } ArrayList<String>().apply { add("testApply") add("testApply") add("testApply") println("this = " + this) }.let { println(it) }}// 运行结果// this = [testApply, testApply, testApply]// [testApply, testApply, testApply]
编译过后的class文件
public static final void testApply() { ArrayList localArrayList1 = new ArrayList(); ArrayList localArrayList2 = (ArrayList)localArrayList1; int $i$a$1$apply; ArrayList $receiver; $receiver.add("testApply"); $receiver.add("testApply"); $receiver.add("testApply"); String str = "this = " + $receiver; System.out.println(str); localArrayList1 = localArrayList1; ArrayList it = (ArrayList)localArrayList1; int $i$a$2$let; System.out.println(it); }
with
with函数是一个单独的函数,并不是Kotlin中的extension,所以调用方式有点不一样,返回是最后一行,然后可以直接调用对象的方法,感觉像是let和apply的结合。
fun <T, R> with(receiver: T, f: T.() -> R): R = receiver.f()
代码示例:
fun testWith() { // fun <T, R> with(receiver: T, f: T.() -> R): R = receiver.f() with(ArrayList<String>()) { add("testWith") add("testWith") add("testWith") println("this = " + this) }.let { println(it) }}// 运行结果// this = [testWith, testWith, testWith]// kotlin.Unit
class文件
public static final void testWith() { Object localObject = new ArrayList(); ArrayList localArrayList1 = (ArrayList)localObject; int $i$a$1$with; ArrayList $receiver; $receiver.add("testWith"); $receiver.add("testWith"); $receiver.add("testWith"); String str = "this = " + $receiver; System.out.println(str); localObject = Unit.INSTANCE; Unit it = (Unit)localObject; int $i$a$2$let; System.out.println(it); }
run
run函数和apply函数很像,只不过run函数是使用最后一行的返回,apply返回当前自己的对象。
fun <T, R> T.run(f: T.() -> R): R = f()
代码示例
fun testRun() { // fun <T, R> T.run(f: T.() -> R): R = f() "testRun".run { println("this = " + this) }.let { println(it) }}// 运行结果// this = testRun// kotlin.Unit
class文件
public static final void testRun() { Object localObject = "testRun"; String str1 = (String)localObject; int $i$a$1$run; String $receiver; String str2 = "this = " + $receiver; System.out.println(str2); localObject = Unit.INSTANCE; Unit it = (Unit)localObject; int $i$a$2$let; System.out.println(it); }
总结
怎么样,是不是看晕了,没关系,我们来总结下。
0 0
- Kotlin之let,apply,with,run函数区别
- Kotlin之let,apply,with,run函数区别
- Kotlin之let,apply,with,run函数区别
- Kotlin之let,apply,with,run函数区别
- 简述Kotlin中let, apply, run, with的区别
- Kotlin:run、apply、let、also、with的用法和区别
- Kotlin基础篇之let、with、run、apply、also函数的使用
- Kotlin学习笔记(2):run、apply、let、also、with的用法和区别
- Kotlin回调的使用以及let、also、with、run、apply函数的用法
- Kotlin简明学习,标准库中的let,apply,with,run方法
- kotlin 语法分析(五)-- with ,let ,apply
- Koltin-let apply also run with的比较
- Kotlin学习之函数
- Kotlin之构造函数
- call/apply let const
- kotlin 踩坑实录(一)之apply
- Kotlin函数之基础函数
- JavaScript之call() 、apply() 函数
- Groovy入门-语法规范
- 安装home-brew下载失败解决失败
- android开发在锁屏界面显示悬浮窗
- SAPUI5 (15) - 绝对绑定和相对绑定
- 将字符串时间转为毫秒值
- Kotlin之let,apply,with,run函数区别
- Hololens SpatialMapping(空间映射) 组件 机理分析(一)
- CentOS 7 下使用 RPM 安装 MySQL5.7
- 5 Ways To Fix Slow 802.11n Speed
- poj2104 Kth-Number
- 计划任务--Crontab
- 二分+单调栈 SPOJ MINSUB
- poj2836(状压dp)
- 常用SQL语言的类型