栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【kotlin】kotlin内置函数run、with、apply、also、let

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【kotlin】kotlin内置函数run、with、apply、also、let

run、with、apply、also、let

这五个函数作用基本一致,就是运行闭包中的代码并返回值,只有在用法上有一些区别。

用法示例:
现有Person类定义如下:

class Person(
        var name: String = "bob",
        var age: Int = 15,
        var sex: Char = '男',
        var height: Int = 194,
        var weight: Int = 88
)

以下代码示意了这五个内置函数的用法

    val human = Person()
    val result1 = human.run {
        name = "alice"
        name // 返回值
    }
    val result2 = with(human) {
        age = 12
        age // 返回值
    }
    val result3 = human.let {
        it.sex = '女'
        it.sex // 返回值
    }
    val result4 = human.also {
        it.height = 166
        it.height // 此句无效,将返回原对象
    }
    val result5 = human.apply {
        weight = 55
        weight // 此句无效,将返回原对象
    }
    println("result1: $result1n" +
            "result2: $result2n" +
            "result3: $result3n" +
            "result4: $result4n" +
            "result5: $result5")

打印结果:

result1: alice
result2: 12
result3: 女
result4: Person@133314b
result5: Person@133314b

在闭包内可以直接使用这个对象的属性或者方法(即this指代这个对象),称这个对象为接收器。
如果闭包内的it(也可以自定义)指代这个对象,那么它就作为参数。

以下表格列举了除了with以外的4个函数:

原对象角色返回值
run接收器闭包返回值
apply接收器原对象
also参数原对象
let参数闭包返回值

with和run基本等价,唯一的区别是run是扩展函数,with是独立的函数。

    // 这两种写法等价
    with(obj) {  }
    obj.run {  }

repeat(n, block)

重复n次执行代码块。相当于fori,实际上实现也是用的fori。

    // 以下二者等价
    repeat(100) { println("hello world") }
    for(i in 1..100) { println("hello world") }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/692814.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号