Kotlin中特殊函数 run()、apply()、let()、also()、with()

Kotlin中特殊函数
  • run() 函数
@kotlin.internal.InlineOnly
public inline fun <T, R> T.run(block: T.() -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return block()
}

最后一行return的 block() 其实就是传入的参数,一般情况下是一个 Lambda 代码块
调用如下示例

 fun MyRun(): String {
        println("testRun 被执行")
        return "testRun 返回值"
    }


    fun testRun() {
        run { MyRun() }
        println("--------------------")
        run { println(MyRun()) }
        println("--------------------")
        run { println("A") }
    }

    @JvmStatic
    fun main(args: Array<String>) {
        testRun()
    }

执行结果如下

testRun 被执行
--------------------
testRun 被执行
testRun 返回值
--------------------
A

  • apply() 函数
@kotlin.internal.InlineOnly
public inline fun <T> T.apply(block: T.() -> Unit): T {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    block()
    return this
}

最后两行,显示调用了block() 函数,然后返回当前的调用者this对象。测试代码如下


 fun testApply(){
 	//普通方式添加数组数据
        val list = mutableListOf<String>()//可以改变自身大小的数组
        list.add("a")
        list.add("b")
        list.add("c")
        println(list)
        println("--------------------")
        //使用apply方式
        val aList = ArrayList<String>().apply {
            this.add("A") //this就是apply的调用者 也就是ArrayList
            add("B")//this可以省略
            add("C")
        }
         println(aList) // 等价于    aList.let { println(it) }
   }

输出结果如下

[a, b, c]
--------------------
[A, B, C]
  • let() 函数
@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return block(this)
}

最后一行代码,将this调用者传入了 block() 函数

fun str(): String {
        return "str"
    }

    fun testLet() {
        "a".let { println(it) }
        1.let { println(it) }
        str().let { println(it) }
    }

    @JvmStatic
    fun main(args: Array<String>) {
        testLet()
    }

输出结果如下

a
1
str

  • also() 函数
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.also(block: (T) -> Unit): T {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    block(this)
    return this
}

先调用了block(this),类似于let()函数逻辑,但最后返回的this

    fun str(): String {
        return "hello"
    }

    fun testAlso() {
        "a".also { println(it) }
        str().also { println(it) }
    }

    @JvmStatic
    fun main(args: Array<String>) {
        testAlso()
    }

输出结果如下

a
hello

  • with() 函数
@kotlin.internal.InlineOnly
public inline fun <T, R> with(receiver: T, block: T.() -> R): R {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    return receiver.block()
}

传入了一个 receiver 接收对象,然后用receiver去调用传入的 block 表达式

fun testWith() {
        val list = mutableListOf<String>()
        list.add("a")
        list.add("b")
        list.add("c")
        println("使用常规写法----- " + list)

        val wList = with(ArrayList<String>()) {
            add("A")
            add("B")
            add("C")
            println("使用with写法----- " + this)
        }.let { println(it) }//kotlin.Unit

    }

    @JvmStatic
    fun main(args: Array<String>) {
        testWith()
    }

输出结果如下

使用常规写法----- [a, b, c]
使用with写法----- [A, B, C]
kotlin.Unit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值