Kotlin中let,apply,run,with和also内置函数的理解与区别

Kotlin中let,apply,run,with和also内置函数的理解与区别,第1张

Kotlin中let,apply,run,with和also内置函数的理解与区别
内置函数的总结
let:
1.let函数返回类型,是根据匿名函数的最后一行变化而变化
2.let函数中的匿名函数里面持有的是it == 集合自身

apply: info:apply
1.apply函数返回类型,都是info自身
2.apply函数中的匿名函数里面持有的是this == info自身

run:str.run
1.run函数返回类型,是根据匿名函数最后一行变化而变化
2.run函数中的匿名函数里面持有的是this == str自身

with with(str)
1.with函数返回类型,是根据匿名函数最后一行变化而变化
2.with函数中的匿名函数里面持有的是this == str自身.
3.跟run在使用的时候不一样

also str.also
1.also函数返回类型,都是str自身
2.also函数中的匿名函数里面持有的是it == str自身.
fun main(args: Array) {

    letMethod()
    
    applyMethod()
    
    runMethod()
    
    withMethod()
    
    alsoMethod()
}

fun letMethod() {
    //let内置函数 对集合第一个元素相加
    val result = listOf(6, 3, 7, 8, 4, 2).let {
        //it == list集合
        it.first() + it.first()//匿名函数的最后一行作为返回值
    }
    println(result)
    println()
    //let方式+空合并 *** 作符 对值判断null并返回
    println(getMethod(""))
    fun getMethod(value: String?): String {
        return value?.let { "传进来的值为it:$it" } ?: "传进来的值为null"
    }
    println(getMethod(null))
    println(getMethod1(""))
}

fun getMethod(value: String?): String {
    return value?.let { "传进来的值为it:$it" } ?: "传进来的值为null"
}

//简化
fun getMethod1(value: String?) = value?.let { "传进来的值为it:$it" } ?: "传进来的值为null"


fun applyMethod() {
    //apply内置函数的方式
    //info.apply特性:始终返回info本身String类型
    val info = "YuKnight Kotlin"
    val infoNew: String = info.apply {
        //大部分情况下匿名函数都会持有一个it,但是apply函数持有当前对象info自身this
        println("apply匿名函数里面打印:$this")
        println("info字符串的长度是:$length")
        println("info全部转成小写:${toLowerCase()}")
    }
    println("apply返回的值:$infoNew")
    //
    info.apply { println("apply匿名函数里面打印:$this") }
        .apply { println("info字符串的长度是:$length") }
        .apply { println("info全部转成小写:${toLowerCase()}") }
    //普通写法
    val file: File = File("D:\a.txt")
    file.setExecutable(true)
    file.setReadable(true)
    println(file.readLines())
    //apply写法
    //匿名函数里面 持有的this == file本身
//    val file1 = File("D:\a.txt")
    file.apply {
        setExecutable(true)
        setReadable(true)
        println(file.readLines())
    }
}


fun runMethod() {
    val str = "yuknight"
    val r = str.run {
        //this == str
        true
    }
    println(r)
    //run中匿名函数
    val strRun = str.run {
        str.length > 5
    }
        .run {
            if (this) "字符串合格" else "字符串不合格"
        }
        .run {
            "[$this]"
        }
    println(strRun)
    //run中使用具名函数
    val strRun1 = str.run(::isLong)
        .run(::showText)
        .run(::mapText)
    println(strRun1)

}

fun isLong(str: String) = str.length > 5
fun showText(isLong: Boolean) = if (isLong) "字符串合格" else "字符串不合格"
fun mapText(getShow: String) = "[$getShow]"


fun withMethod() {
    val str = "yuknight"
    val length = with(str) {
        this.length//this == str  //返回类型为匿名函数里面最后一行
    }
    println(length)

    val r1 = with(str, ::getStrLen)
    val r2 = with(r1, ::getLenInfo)
    val r3 = with(r2, ::getInfoMap)
    with(r3, ::getshow)
    println()
    //匿名 *** 作
    with(with(with(with(str) {
        length
    }) {
        "字符长度:$this"
    }) {
        "[$this]"
    }) {
        println(this)
    }
}

fun getStrLen(str: String) = str.length
fun getLenInfo(len: Int) = "字符长度:$len"
fun getInfoMap(info: String) = "[$info]"
fun getshow(content: String) = println(content)


fun alsoMethod() {
    val str = "YuKnight"
    str.also {
        it.length  //it == str
        println("$it") //返回值为str自身
    }
    //str.also特点,可以链式调用
    str.also {
        println("str原始数据是:$it")
    }.also {
        println("str转换成小写:${it.toLowerCase()}")
    }.also {
        println("链式调用结算")
    }
    //
    val file = File("D:\a.txt")
    file.also {
        it.setReadable(true)
        it.setWritable(true)
        println(it.readLines())
    }.also {
        it.setReadable(true)
        println(it.readLines())
    }.also {
        it.setReadable(true)
        println(it.readLines())
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5721927.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存