【Kotlin】let,also,run,apply与with的使用去区别

以下例子我们将前置代码的返回值称为 T,花括号部分称为 block,以弹出桌面程序选择框为例:

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.
  • 百度翻译:以 this 值作为 参数 调用指定的函数 [block],并返回 this 值。
  • 个人理解:将 T 作为 it 传入 block 并返回 T
intent.also {
    
    //it:Intent
    it.addCategory(Intent.CATEGORY_HOME)
    it.setClassName("android", "com.android.internal.app.ResolverActivity")
    startActivity(it)
}.let{
    
    //it:Intent
}

3、run – block: T.() -> R

  • Calls the specified function [block] with this value as its receiver and returns its result.
  • 百度翻译:以 this 值作为其 接收器 调用指定的函数 [block],并返回其结果。
  • 个人理解:将 T 作为 this 传入 block 并返回 block 最后一行结果,this 可以省略
intent.run {
    
    //this:Intent
    addCategory(Intent.CATEGORY_HOME)
    setClassName("android", "com.android.internal.app.ResolverActivity")
    startActivity(this)
}.let{
    
    //this:Unit
}

4、apply – block: T.() -> Unit

  • Calls the specified function [block] with this value as its receiver and returns this value.
  • 百度翻译:以 this 值作为其 接收器 调用指定的函数 [block],并返回 this 值。
  • 个人理解:将 T 作为 this 传入 block 并返回 T
intent.apply {
    
    //this:Intent
	addCategory(Intent.CATEGORY_HOME)
    setClassName("android", "com.android.internal.app.ResolverActivity")
    startActivity(this)
}.let{
    
    //this:Intent
}
//可以这么写,因为apply返回值还是intent,执行顺序也是先apply再startActivity
startActivity(Intent(Intent.ACTION_MAIN).apply {
    
    
    addCategory(Intent.CATEGORY_HOME)
    setClassName("android", "com.android.internal.app.ResolverActivity")
    activity?.finish()
})

5、with – receiver: T, block: T.() -> R

  • Calls the specified function [block] with the given [receiver] as its receiver and returns its result.
  • 百度翻译:以给定的 [receiver] 作为其 接收器 调用指定的函数 [block],并返回其结果。
  • 个人理解:将接收部分(T)作为 this 传入 block 并返回 block 最后一行结果
with(Intent(Intent.ACTION_MAIN)){
    
    //this:Intent
    addCategory(Intent.CATEGORY_HOME)
    setClassName("android", "com.android.internal.app.ResolverActivity")
    startActivity(this)
}.let{
    
    //this:Unit
}

猜你喜欢

转载自blog.csdn.net/qq_36881363/article/details/114393032