在本教程中,我们将实施 Kotlin 中提供的一些重要的标准图书馆功能。kotlin-stdlib
为我们提供有用的(社区/教程/kotlin-lambda-higher-order-functions) idiomatic 模式。
- let
- run
- also
- apply
- with
科特林飞行
let
将它所召唤的对象作为参数,并返回 lambda 表达式的结果. Kotlin let 是一个范围函数,在该表达式中声明的变量不能用于外部。
1fun main(args: Array<String>) {
2 var str = "Hello World"
3 str.let { println("$it!!") }
4 println(str)
5
6}
7//Prints
8//Hello World!!
9//Hello World
'it' 关键字包含在 'let' 中属性的副本. let 的最后一个值作为参数返回,如下所示。
1var strLength = str.let { "$it function".length }
2println("strLength is $strLength") //prints strLength is 25
链接让功能
1var a = 1
2var b= 2
3
4a = a.let { it + 2 }.let { val i = it + b
5 i}
6println(a) //5
As you can see we've declared a local variable "i" inside the second let function. Setting the last statement of the let function to i
returns the property to the outer property a
.
尼泊尔飞行
我们可以设置一个 let 表达式在另一个 let 表达式中,如下所示。
1var x = "Anupam"
2x.let { outer -> outer.let { inner -> print("Inner is $inner and outer is $outer") } }
3
4//Prints
5//Inner is Anupam and outer is Anupam
对于嵌入式 let,我们不能使用it
关键字. 我们需要在两个 let 函数中为it
分配明确的名称. 只有最外部的 let 返回如下所示的值。
1var x = "Anupam"
2 x = x.let { outer ->
3 outer.let { inner ->
4 println("Inner is $inner and outer is $outer")
5 "Kotlin Tutorials Inner let"
6 }
7 "Kotlin Tutorials Outer let"
8 }
9 println(x) //prints Kotlin Tutorials Outer let
让零检查
此外, let 对于检查 [ 可取消属性]( / 社区 / 教程 / kotlin-null-safety-nullable)有用,如下所示。
1var name : String? = "Kotlin let null check"
2name?.let { println(it) } //prints Kotlin let null check
3name = null
4name?.let { println(it) } //nothing happens
在 let 表达式中的代码只在属性不是 null 时执行,因此 let 将我们从 如果没有 null 检查器中拯救!
牛仔跑
Kotlin运行
是另一个有趣的函数,下面的示例展示了它的用例。
1var tutorial = "This is Kotlin Tutorial"
2 println(tutorial) //This is Kotlin Tutorial
3 tutorial = run {
4 val tutorial = "This is run function"
5 tutorial
6 }
7 println(tutorial) //This is run function
Kotlin run expression can change the outer property. Hence in the above code, we've redefined it for the local scope.
- 与 let 函数相似, run 函数还返回最后一个语句
- 与 let 不同, run 函数不支持
it
关键字
让和奔跑
让我们将 let 和运行函数结合起来。
1var p : String? = null
2 p?.let { println("p is $p") } ?: run { println("p was null. Setting default value to: ")
3 p = "Kotlin"}
4
5 println(p)
6//Prints
7//p was null. Setting default value to:
8//Kotlin
科特林
正如其名称所示,也
表达式对被召唤的对象进行了一些额外的处理,与也
不同,它返回原始对象而不是任何新的返回数据,因此返回数据总是具有相同的类型。
1var m = 1
2m = m.also { it + 1 }.also { it + 1 }
3println(m) //prints 1
Kotlin Let vs. 同样
以下代码片段显示了一个很好的例子来区分让我们
和也
。
1data class Person(var name: String, var tutorial : String)
2var person = Person("Anupam", "Kotlin")
3
4var l = person.let { it.tutorial = "Android" }
5var al = person.also { it.tutorial = "Android" }
6
7println(l)
8println(al)
9println(person)
In the above code, we've used Data classes. The also expression returns the data class object whereas the let expression returns nothing (Unit) as we didn't specify anything explicitly.
Kotlin 应用
Kotlin 'apply' 是一个类型的扩展函数,它在表达式中运行对象引用(也称为接收器)并在完成时返回对象引用。
1data class Person(var name: String, var tutorial : String)
2var person = Person("Anupam", "Kotlin")
3
4person.apply { this.tutorial = "Swift" }
5println(person)
应用VS
1data class Person(var n: String, var t : String)
2var person = Person("Anupam", "Kotlin")
3
4person.apply { t = "Swift" }
5println(person)
6
7person.also { it.t = "Kotlin" }
8println(person)
Note: In apply
it
isn't allowed. If the property name of the data class is unique in the function, you can omit this
. We should use also
only when we don't want to shadow this
.
Kotlin 与
例如应用
,与
用于更改实例属性,而无需每次调用 dot操作员来引用。
1data class Person(var name: String, var tutorial : String)
2var person = Person("Anupam", "Kotlin")
3
4with(person)
5 {
6 name = "No Name"
7 tutorial = "Kotlin tutorials"
8 }
Again
with
is similar to apply
except for a few differences.
Kotlin 应用 vs 与
- 使用运行没有对象(接收器)而应用需要一个
- 应用运行对象引用,而使用只通过它作为一个参数
- 用函数的最后一个表达式返回一个结果
1var xyz = with(person)
2 {
3 name = "No Name"
4 tutorial = "Kotlin tutorials"
5 val xyz = "End of tutorial"
6 xyz
7 }
8 println(xyz) //End of tutorial
这就是Kotlin标准函数改变变量或修改函数中的对象的全部。