Kotlin 操作符:run、with、let、also、apply 的差异与选择

81 篇文章 3 订阅

转自原文链接

Kotlin 操作符:run、with、let、also、apply 的差异与选择

Kotlin 操作符:run、with、let、also、apply 的差异与选择


Kotlin 的一些操作符非常相似,我们有时会不确定使用哪种功能。在这里我将介绍一个简单的方法来清楚地区分他们的差异,最后以及如何选择使用。

首先我们以下这个代码:

class MyClass {
    fun test() {
        val str: String = "Boss"
        val result = str.let {
             print(this) // 接收者
             print(it) // 参数
             69 //区间返回值
        }
        print(result)
    }
}复制代码

在上面个例子中,我们使用了 let 操作符,当使用这个函数时,我们所需要问题的有三点:

  • this 的含义(接收器)
  • it 的含义(参数)
  • result 最终得到的什么

因为我们使用的是 let,所以对应是:

  • 『 this 』为『 this @ MyClass 』, this 是 MyClass 的实例,因为 test() 是 MyClass 的一个方法。而如果 test() 是一个空函数(Free function —— 不附加到任何类),将会出现一个编译错误。
  • 『 it 』为原字符串本身『 Boss 』
  • 『 result 』是数字 69,我们已经从区间返回赋值

我们用表格更直观的作显示:

操作符接收者(this)传参(it)返回值(result)
letthis@MyclassString( "Boss" )Int( 69 )

依此类推:我们可以这段代码为其余的功能做类似的事情。

以下是测试操作符通用的代码,你可以使用 let、run、apply、also 中任何的操作符替换 xxx

class MyClass {
    fun test() {
        val str: String = "Boss"
        val result = str.xxx {
             print(this) // 接收者
             print(it) // 参数
             69 //区间返回值
        }
        print(result)
    }
}复制代码

返回值为:

操作符接收者(this)传参(it)赋值(result)
T.let()this@MyclassString( "Boss" )Int( 69 )
T.run()String( "Boss" )编译错误Int( 69 )
T.apply()String( "Boss" )编译错误String( "Boss" )
T.also()this@MyclassString( "Boss" )String( "Boss" )

with 与 also 操作符在使用上有一些细微的差异,例如:

class MyClass {
    fun test() {
        val str: String = "Boss"
        val result = with(str) {
             print(this) // 接收者
            // print(it) // 参数
            69 //区间返回值
        }
        print(result)
    }
}复制代码
class MyClass {
    fun test() {
        val str: String = "Boss"
        val result = run  {
            print(this) // 接收者
            // print(it) // 参数
            69 //区间返回值
        }
         print(result)
    }
}复制代码

 

操作符接收者(this)传参(it)返回值(result)
T.let()this@MyclassString( "Boss" )Int( 69 )
T.run()String( "Boss" )编译错误Int( 69 )
with(T)String( "Boss" )编译错误Int( 69 )
T.apply()String( "Boss" )编译错误String( "Boss" )
T.also()this@MyclassString( "Boss" )String( "Boss" )

而关于什么时候我们应该用到什么操作符,可以参考这个图:

 

Function_selections

Function_selections

 

参考文章:

  1. Mastering Kotlin standard functions: run, with, let, also and apply
  2. The difference between Kotlin’s functions: ‘let’, ‘apply’, ‘with’, ‘run’ and ‘also’
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值