码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Kotlin内置函数之apply、let、run、with


    目录

    总结:

    一、apply 用法: info.apply{}

    二、run用法 : info.run{}

    三、let用法:info.let{}

    四、with用法 : with(info)

    五、also:info.also{} //返回info本身



    总结:

    apply、let、run、with是kotlin里面常见的内置函数

    apply 里面可以使用this,返回this

    run里面可以使用this,返回自定义

    let 里面不可以使用this,返回自定义

    with和run使用场景一样,只不过和run的调用方式不一样

    also里面可以使用it,返回调用对象本身。

    一、apply 用法: info.apply{}

    1、apply函数返回类型,返回info本身(this)

    2、apply函数的匿名函数里面持有的this == info,就是info本身(因为T.()的原因)

    1. data class ApplyTest(var name : String , var age : Int)
    2. fun main(){
    3. var name : String? ;
    4. name = "lidongxu" ;
    5. var applyTest = ApplyTest("menshen" , 34)
    6. applyTest.apply {
    7. println("Apply CodeBlock LetTest1 name = {$name} , age = {$age}")
    8. this.age = 100
    9. println("Apply CodeBlock LetTest2 name = {$name} , age = {$age}")
    10. }
    11. println("applyTest name = {${applyTest.name} , age = {${applyTest.age}")
    12. //给StringBuilder扩展一个apply函数
    13. fun StringBuilder.myApply(block: StringBuilder.() ->Unit) : StringBuilder {
    14. block()
    15. return this
    16. }
    17. var sb = StringBuilder("123")
    18. sb.myApply {
    19. append("456")
    20. append("654")
    21. }
    22. println(sb)
    23. }
    24. /**
    25. * 打印结果
    26. * Apply CodeBlock LetTest1 name = {macoli} , age = {34}
    27. * Apply CodeBlock LetTest2 name = {macoli} , age = {100}
    28. * applyTest name = {menshen , age = {100}
    29. * 123456654
    30. */

    二、run用法 : info.run{}

    1、run函数返回类型,根据最后一行而定

    2、run函数的匿名函数里面持有的this == info,就是info本身(因为T.()的原因)

    //除了返回值不一样,其余和上面例子一样的

    三、let用法:info.let{}

    1、let函数返回类型,根据最后一行而定

    2、let函数内it相当于info

    1. //使用let做空判断
    2. fun main(){
    3. println(judgeMethod("麦克LI"))
    4. }
    5. fun judgeMethod(value : String?) : String {
    6. return value?.let {
    7. "欢迎光临 , $it"
    8. } ?: "啥都没有进来干啥"
    9. }

    四、with用法 : with(info)

    with和run一样,只不是调用方式不一样,with相当于是全局函数

    1. fun main(){
    2. var test = "李东旭"
    3. var v1 = with(test , ::getStrLen)
    4. var v2 = with(v1 , ::getStrLenStr)
    5. println(v2)//输出"test长度是 $value"
    6. }
    7. fun getStrLen(value : String) = value.length
    8. fun getStrLenStr(value : Int) = "test长度是 $value"

    五、also:info.also{} //返回info本身

    1. fun main(){
    2. alsoTest()
    3. }
    4. fun alsoTest() {
    5. var r = "ILoveChina".also {
    6. println("ALSO CODE BLOCK $it")
    7. println("ALSO CODE BLOCK ${it.toUpperCase()}")
    8. it.forEach {
    9. print("CHAR $it , ")
    10. }
    11. println()
    12. }
    13. println("OUTOF ALSO CODE BLOCK $r" )
    14. }

  • 相关阅读:
    实现数字化转型的解决方案
    非时间参考移动机器人路径跟踪
    Workfine新手入门:分级预览
    人工智能,丹青圣手,全平台(原生/Docker)构建Stable-Diffusion-Webui的AI绘画库教程(Python3.10/Pytorch1.13.0)
    14:STM32-----看门狗
    leetcode 1207. Unique Number of Occurrences(出现次数的唯一性)
    【分布式】 A survey on the distributed computing stack论文小抄
    Java中的命名规则
    【数据库】函数处理(文本处理函数、日期和时间处理函数、数值处理函数)
    DO280管理和监控OpenShift平台--使用probes监视应用
  • 原文地址:https://blog.csdn.net/mldxs/article/details/127134124
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号