doc: update docs/kotlin.md (#331)

This commit is contained in:
萌新杰少 2023-03-16 15:52:17 +08:00 committed by GitHub
parent 1eadc7748e
commit cb293dc036
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -606,6 +606,75 @@ fun main() {
``` ```
<!--rehype:className=wrap-text--> <!--rehype:className=wrap-text-->
### 简单的高阶函数
<!--rehype:wrap-class=col-span-2-->
```kotlin
//注意啦这里的num1AndNum2有个operation它是接收了一个函数作为形参
fun num1AndNum2(num1: Int, num2: Int, operation: (Int, Int) -> Int): Int {
//让我们试着向operation传入参数
return operation(num1, num2)
}
fun plus(num1: Int, num2: Int): Int {
return num1 + num2
}
fun main(args: Array<String>) {
val total = num1AndNum2(100, 200, ::plus)
println(total)//300
//怎么样?我们利用传入一个函数来充当另一个函数的参数
}
```
还记得我们怎么在Java中用接口吗
试着用函数参数简化它
<!--rehype:className=wrap-text-->
### 以匿名函数作为参数的函数
```kotlin
//operation是一个函数类型的参数哦
fun num1AndNum2(num1: Int, num2: Int, operation: (Int, Int) -> Int): Int {
return operation(num1, num2)
}
fun main(args: Array<String>) {
//这里我们定义一个匿名函数
val operation: (Int, Int) -> Int = { i: Int, i2: Int ->
i + i2
}
val total = num1AndNum2(100, 200, operation)
println(total) //300
}
```
<!--rehype:className=wrap-text-->
### Lambda表达式方式传入函数参数
<!--rehype:wrap-class=col-span-2-->
```kotlin
//我们还是不改变什么
fun num1AndNum2(num1: Int, num2: Int, operation: (Int, Int) -> Int): Int {
return operation(num1, num2)
}
fun main(args: Array<String>) {
//wow哦天哪Lambda可以做到这样简洁
val total = num1AndNum2(100, 200) { n1, n2 ->
n1 + n2
}
println(total)
}
```
这里之所以可以把lambda写在外部是因为operation是最后一个参数。
<!--rehype:className=wrap-text-->
--- ---