Kotlin简明学习,标准库中的let,apply,with,run方法
来源:互联网 发布:网络歌手靠什么赚钱 编辑:程序博客网 时间:2024/06/06 07:04
Koltln标准库中有几个反方法很容易搞混,对初学者很难理解。他们分别是let,apply,with,run方法。熟练的使用他们可以让我们的代码更加优雅和可读
let方法
我用的Kotlin的标准库的版本是1.1.2-4,看看let方法的定义
/** * Calls the specified function [block] with `this` value as its argument and returns its result. */@kotlin.internal.InlineOnlypublic inline fun <T, R> T.let(block: (T) -> R): R = block(this)
我来分析下这个let方法的定义(个人观点,不对请雅正)
首先,这个let方法是一个扩展方法。是谁的扩展方法呢?是类型T的扩展方法。这些方法的定义都用到了泛型。所以我们写let方法的时候是这样写的:类实例+点+let。
其次,这个let方法接收一个参数,参数是个方法。这个方法的名字叫block。let接收的方法是一个穿入值是T类型返回值是R类型的方法。并且我们知道这个方法的反回值就是整个let方法的返回值。绕口,见谅。
最后,block(this)是整个let方法的定义,记住let方法是有返回值的。
Example:
val result ="Hello World".let { println(this) println(it) 520 } println(result)
“Hello Wrold”就是我们let定义中的T。是一个String类型的对象。一对大挂号是已个匿名方法。我们也叫lambda表达式。最后一行520是这个匿名函数的返回值,也是let方法的返回值,所以result的值应该是520。我们在使用let方法的时候一般不在在乎这个let方法的返回值,我们主要是通过let方法对某个对象的进行设置。
Example的打赢结果
System.out: com.example.kotlintest.MainActivity@4df51d4System.out: Hello WorldSystem.out: 520
it是Kotlin的一个关键字,在这个例子中代表T,因为lanmbda表达式只有一个参数,我们可以用it来代替这个参数。整段代码我是写在onCreate方法中的所以这个println(this)中的this代表的是MainActivity这个对象。讲好累let讲完了,不知道你有没有理解。知道了let下面几个就容易理解多了。很类似但也不同。
Koltin和Java的比较
// Javaif (message != null) { System.out.println(message)}// Kotlin 如果message是null下面这行忽略message?.let { println(it) }
apply方法
首先看看apply方法的定义
/** * Calls the specified function [block] with `this` value as its receiver and returns `this` value. */@kotlin.internal.InlineOnlypublic inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }
apply方法是类型T的方法扩张方法
apply方法有有个参数,这个参数也是T的扩展函数。这样当我们用lambda表达式去调用这个方法的时候,this就表示这个类型T的对象了。在apply方法的方法体中我们看到有两行代码,分别是block(this)和return this。所以我们知道这个apply方法返回的就是这个T对象。block()表示对参数方法的调用。
举个例子
val result ="Hello World".apply { println(this)// println(it) // 编译器报错,lambda表达式没有参数it不能用 520}println(result)
看下打印的结果:
System.out: Hello WorldSystem.out: Hello World
with方法
先看定义
/** * Calls the specified function [block] with the given [receiver] as its receiver and returns its result. */@kotlin.internal.InlineOnlypublic inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
- with方法不是扩张方法
- with方法有两个参数一个是类型位T的对象,一个是方法参数。这个方法参数是类型T的扩展方法,所以用lambda作为参数传入的时候可以持有T的引用,就是所this代表的是T对象。
- with方法的返回值是block方法的返回值
举个例子
val result = with("Hello World") { println(this)// println(it) // 编译器报错,lambda表达式没有参数it不能用 520}println(result)
打印结果
System.out: Hello WorldSystem.out: Hello World
run方法
定义
/** * Calls the specified function [block] with `this` value as its receiver and returns its result. */@kotlin.internal.InlineOnlypublic inline fun <T, R> T.run(block: T.() -> R): R = block()
run方法和apply方法很类似,就是返回值不同
直接例子。想想……
例子
val result = "Hello World".run { println(this)// println(it) // 编译器报错,lambda表达式没有参数it不能用 520}println(result)
打印结果
System.out: Hello WorldSystem.out: 520
Kotlin简明学习—文章列表
- Kotlin简明学习,标准库中的let,apply,with,run方法
- Kotlin学习笔记(2):run、apply、let、also、with的用法和区别
- Kotlin之let,apply,with,run函数区别
- Kotlin之let,apply,with,run函数区别
- 简述Kotlin中let, apply, run, with的区别
- Kotlin之let,apply,with,run函数区别
- Kotlin之let,apply,with,run函数区别
- Kotlin:run、apply、let、also、with的用法和区别
- Kotlin回调的使用以及let、also、with、run、apply函数的用法
- Kotlin基础篇之let、with、run、apply、also函数的使用
- kotlin 语法分析(五)-- with ,let ,apply
- Koltin-let apply also run with的比较
- Kotlin简明学习,infix函数
- Kotlin学习笔记——标准库kotlin
- Kotlin学习笔记——标准库kotlin.system
- Kotlin学习笔记——标准库kotlin.io
- Kotlin学习笔记——标准库kotlin.collections
- Kotlin简明学习—文章列表
- 讯飞语音唤醒和语义识别同时使用,错误代码20006的解决方法
- 创建型模式之抽象工厂(Abstract Factory)
- CC3200 wifi应用基础——中文教程
- HDU3746--Cyclic Nacklace (Kmp)
- cocos2dx打包错误记录
- Kotlin简明学习,标准库中的let,apply,with,run方法
- 怎样提高Java编程效率?
- epoll server client
- linux 使用笔记1
- maven出现错误:中不到jconsole怎么办?
- linux 使用笔记2
- linux 使用笔记3
- linux使用笔记4
- gis笔记 wms wfs等OGC标准