使用扩展函数、Lambda 与集合操作。
本节要点
- fun 定义函数
- 扩展函数为已有类型添加方法
- map/filter 链式处理集合
代码示例
fun String.shout() = uppercase() + "!"
val nums = listOf(1, 2, 3)
val doubled = nums.map { it * 2 }
println("kotlin".shout())提示:扩展函数不会真正修改类型,只在调用处生效。
使用扩展函数、Lambda 与集合操作。
fun String.shout() = uppercase() + "!"
val nums = listOf(1, 2, 3)
val doubled = nums.map { it * 2 }
println("kotlin".shout())