kotlin: let / run / also / apply

package com.njp.coroutinesdemo

import org.junit.Test

import org.junit.Assert.*

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class Test {
    
    private var user: User? = null

    /**
     *  run
     *  {} 内为this
     *  返回值默认Unit,可指定返回
     *  public inline fun <T, R> T.run(block: T.() -> R): R {
     */
    @Test
    fun test1() {
        user = User("xq", 18)

        val run1 = user?.run {
            name = "qx"
            printLog("run : $this")
            this
        }
        printLog("返回值 : $run1")

        printLog("=============华丽分割线==============")

        val run2 = user?.run {
            name = "qx"
            printLog("run : $this")
//            this
        }
        printLog("返回值 : $run2")
    }
    //==========run : User(name=qx, age=18)===============
    //==========返回值 : User(name=qx, age=18)===============
    //=======================华丽分割线=============================
    //==========run : User(name=qx, age=18)===============
    //==========返回值 : kotlin.Unit===============

    /**
     *  let
     *  {} 内为it
     *  返回值默认Unit,可指定返回
     *  public inline fun <T, R> T.let(block: (T) -> R): R {
     */
    @Test
    fun test2() {
        user = User("xq", 18)

        val let1 = user?.let {
            it.name = "qx"
            printLog("let : $it")
            it
        }
        printLog("返回值 : $let1")

        printLog("=============华丽分割线==============")

        val let2 = user?.let {
            it.name = "qx"
            printLog("let : $it")
//            it
        }
        printLog("返回值 : $let2")
    }
    //==========let : User(name=qx, age=18)===============
    //==========返回值 : User(name=qx, age=18)===============
    //=======================华丽分割线=============================
    //==========let : User(name=qx, age=18)===============
    //==========返回值 : kotlin.Unit===============

    /**
     *  apply
     *  {} 内为this
     *  返回值为对象本身,且不需要执行返回
     *  public inline fun <T> T.apply(block: T.() -> Unit): T {
     */
    @Test
    fun test4() {
        user = User("xq", 18)

        val apply1 = user?.apply {
            name = "qx"
            printLog("apply : $this")
            this    //多余
        }
        printLog("返回值 : $apply1")

        printLog("=============华丽分割线==============")

        val apply2 = user?.apply {
            name = "qx"
            printLog("apply2 : $this")
//            this
        }
        printLog("返回值 : $apply2")
    }
    //==========apply : User(name=qx, age=18)===============
    //==========返回值 : User(name=qx, age=18)===============
    //=======================华丽分割线=============================
    //==========apply2 : User(name=qx, age=18)===============
    //==========返回值 : User(name=qx, age=18)===============

    /**
     *  also
     *  {} 内为it
     *  返回值为对象本身,且不需要执行返回
     *  public inline fun <T> T.also(block: (T) -> Unit): T {
     */
    @Test
    fun test3() {
        user = User("xq", 18)

        val also1 = user?.also {
            it.name = "qx"
            printLog("also : $it")
            it  //多余
        }
        printLog("$also1")

        printLog("=============华丽分割线==============")

        val also2 = user?.also {
            it.name = "qx"
            printLog("also : $it")
//            it
        }
        printLog("$also2")
    }
    //==========also : User(name=qx, age=18)===============
    //==========User(name=qx, age=18)===============
    //=======================华丽分割线=============================
    //==========also : User(name=qx, age=18)===============
    //==========User(name=qx, age=18)===============

    
    fun printLog(s: String) {
        println("==========$s===============")
    }
}

data class User(var name: String = "", var age: Int = 0)


测试二:


        btn1.text = ""
        btn1.setTextColor(Color.parseColor("#ff0000"))
        btn1.textSize = 18f

        //public inline fun <T, R> with(receiver: T, block: T.() -> R): R {
        with(btn1) {
            text = ""
            setTextColor(Color.parseColor("#ff0000"))
            textSize = 18f
            return@with "返回值自定义"
        }

        btn1.run {
            text = ""
            setTextColor(Color.parseColor("#ff0000"))
            textSize = 18f
            return@run "返回值自定义"
        }

        btn1.apply {
            text = ""
            setTextColor(Color.parseColor("#ff0000"))
            textSize = 18f
            return@apply //只能返回自己
        }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值