var intent =Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_HOME)
intent.setClassName("android","com.android.internal.app.ResolverActivity")startActivity(intent)
总结
let 与 also 将 T 作为 it 传入 block,区别在于返回结果分别为 block 最后一行结果与 T本身
run 与 apply 将 T 作为 this 传入 block,区别在于返回结果分别为 block 最后一行结果与 T本身
with 也是将 T 作为 this 传入 block 并返回 block 最后一行结果,区别在于写法是 with(T)
1、let – block: (T) -> R
Calls the specified function [block] with ‘this’ value as its argument and returns its result.
百度翻译:用 this 值作为 参数 调用指定的函数 [block],并返回结果。
个人理解:将 T 作为 it 传入 block 并返回结果(T 为 intent,block 为 let 后的 {} 及内容)
var intent =Intent(Intent.ACTION_MAIN)
intent.let{
//it:Intent
it.addCategory(Intent.CATEGORY_HOME)
it.setClassName("android","com.android.internal.app.ResolverActivity")startActivity(it)//这里无返回值,所以整体let也是无返回值的}.let{
//it:Unit}
2、also – block: (T) -> Unit
Calls the specified function [block] with ‘this’ value as its argument and returns this value.