Kotlin的内置函数 apply、let、run、with、also

let

1.let函数返回类型,是根据匿名函数的最后一行变化而变化
2.let函数中的匿名函数里面持有的是it == 集合自身

java 复制代码
fun main() {
    var num1 = 1
    var num2 = 1
    var result:Int
    result = num1 + num2
    var str = result?.let {//传入本身,it指代本身即result,result不为空则执行let,为空则不执行
         println("传进来的值为${it}")
        "${num1} + ${num2} = ${result}" //此处为返回的值
    }?:"传进来的值为null"
    print(str)
}

传进来的值为2

1 + 1 = 2

apply

apply: info:apply

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

run

run:str.run

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

java 复制代码
fun runFunction(){
    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: Stri

true

字符串合格

字符串合格

with

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

java 复制代码
fun isLong(str: String) = str.length > 5
fun showText(isLong: Boolean) = if (isLong) "字符串合格" else "字符串不合格"
fun mapText(getShow: String) = "[$getShow]"
fun getStrLen(str: String) = str.length
fun getLenInfo(len: Int) = "字符长度:$len"
fun getInfoMap(info: String) = "[$info]"
fun getshow(content: String) = println(content)

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

与run不同的是width需要传入参数

also

also str.also

1.also函数返回类型,都是str自身
2.also函数中的匿名函数里面持有的是it == str自身.

java 复制代码
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("链式调用结算 ${it}")
    }
}

YuKnight

str原始数据是:YuKnight

str转换成小写:yuknight

链式调用结算 YuKnight

总结

方法 返回值 自身指代 是否传参
let 最后一行 it
apply 自身 this
run 最后一行 this
with 最后一行 this
also 自身 it
相关推荐
fundroid9 小时前
Kotlin 泛型进阶:in、out 与 reified 实战
android·开发语言·kotlin
JMchen12321 小时前
现代Android图像处理管道:从CameraX到OpenGL的60fps实时滤镜架构
android·图像处理·架构·kotlin·android studio·opengl·camerax
JMchen1232 天前
Android CameraX深度解析:从Camera1到CameraX的相机架构演进
android·java·数码相机·架构·kotlin·移动开发·android-studio
倔强的石头1062 天前
【Linux指南】进程控制系列(五)实战 —— 微型 Shell 命令行解释器实现
linux·运维·kotlin
Hz4533 天前
Android Jetpack核心组件协同实战:Navigation 3.X+Lifecycle+Flow+Hilt的架构革新
android·kotlin
JMchen1233 天前
Android音频编码原理与实践:从AAC到Opus,深入解析音频编码技术与移动端实现
android·经验分享·学习·kotlin·android studio·音视频·aac
JMchen1233 天前
Android音频处理全解析:从3A算法到空间音频,打造专业级音频体验
android·经验分享·算法·kotlin·android studio·音视频
瓦特what?3 天前
C++编程防坑指南(小说版)
android·c++·kotlin
一招定胜负3 天前
卷积神经网络提取人脸五个特征点
人工智能·cnn·kotlin
HeDongDong-3 天前
详解 Kotlin 的函数
开发语言·python·kotlin