The difference between `Let`,`Ap

2017-08-17  本文已影响0人  Ted熊
kotlin_0

Let


Apply


With


Run


Also

public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this }
```


总结

function des this it return
Let 对象扩展函数,通过it获取对象本身 所属class 对象本身 指定return或者最后一行
Apply 对象扩展函数,在函数块类调用对象的任意方法 对象本身 - 对象本身
With 独立函数,将对象作为参数传入, 对象本身 - 指定return或者最后一行
Run 对象扩展函数,类似Apply,不同之处在于Apply返回自身 对象本身 - 指定return或者最后一行
Also 对象扩展函数,类似Apply,比Apply多了it 所属class 对象本身 对象本身

参考文章:
https://medium.com/@tpolansk/the-difference-between-kotlins-functions-let-apply-with-run-and-else-ca51a4c696b8
项目源码:https://git.tedxiong.com/TedXiong/KotlinSample

上一篇 下一篇

猜你喜欢

热点阅读