val和var
var代表了varible变量,val是varible+final,当然val引用不可变。两者都可以声明一个类的属性。尽可能的采用val、不可变对象及纯函数来设计程序。
高阶函数
以其他函数作为参数或返回值的函数可以理解为高阶函数。
//定义国家类型
data class Country(
val name: String,
val continient: String,
val population: Int
)
class CountryApp {
//筛选出国家是欧洲的
fun filterCountries(countries: List<Country>): List<Country> {
val res = mutableListOf<Country>()
for (c in countries) {
if (c.continient == "EU") {
res.add(c)
}
}
return res
}
//改变策略
fun filterCountries(countries: List<Country>, continient: String): List<Country> {
val res = mutableListOf<Country>()
for (c in countries) {
if (c.continient == continient) {
res.add(c)
}
}
return res
}
//两种条件
fun filterCountries(countries: List<Country>, continient: String, population: Int): List<Country> {
val res = mutableListOf<Country>()
for (c in countries) {
if (c.continient == continient && c.population > population) {
res.add(c)
}
}
return res
}
//改造后的高阶函数,可自定义规则,如将isBigEuropeanCountry传入
fun filterCountries(countries: List<Country>, test: (Country) -> Boolean): List<Country> {
val res = mutableListOf<Country>()
for (c in countries) {
if (test(c)) {
res.add(c)
}
}
return res
}
}
class CountryTest {
fun isBigEuropeanCountry(country: Country): Boolean {
return country.continient == "EU" && country.population > 10000
}
}
//简单调用
fun main(args: Array<String>) {
val countryList = mutableListOf<Country>()
val countryApp = CountryApp()
val countryTest = CountryTest()
countryApp.filterCountries(countryList, countryTest::isBigEuropeanCountry);
}
各种高级参数
//没有参数的函数类型
fun a(u: () -> Unit) {
}
//多个参数
fun a(u: (Int, String) -> Unit) {
}
//指定名称
fun b(u: (errCode: Int, errMsg: String) -> Unit) {
}
//可为空
fun c(u: (errCode: Int, errMsg: String?) -> Unit) {
}
//可选
fun d(u: ((errCode: Int, errMsg: String) -> Unit)?) {
}
//返回一个函数,这里表示传入一个Int的参数,然后返回另一个类型为 (Int) -> Unit 的函数,完全不建议
fun a(u: (Int) -> ((Int) -> Unit)) {
}
:: 用法
//定义Book类
class Book(val name: String)
//定义一个类的构造方法引用变量
val getBook = ::Book
val book: Book = getBook("kotlin")
//引用name方法
print(book::name)
countryList.forEach(System.out::println)
匿名函数
我们可将高阶函数中例子CountryTest#isBigEuropeanCountry改为
countryApp.filterCountries(countryList, fun(country: Country): Boolean {
return country.continient == "EU" && country.population > 10000
});
Lambda语法糖
继续改造,it 表示传入的country对象
countryApp.filterCountries(countryList, {
it.continient == "EU" && it.population > 10000
});
countryApp.filterCountries(countryList) {
it.continient == "EU" && it.population > 10000
};
当然还有
val sum: (Int, Int) -> Int = { x: Int, y: Int ->
x + y
}
val sum1 = { x: Int, y: Int ->
x + y
}
val sum2: (Int, Int) -> Int = { x, y ->
x + y
}
一个Lambda的表达式必须用 {} 包裹。
如果Lambda声明了参数部分的部分,且返回值类型支持类型推导,那么Lambda变量就可以省略函数类型申明。
如果Lambda变量声明了函数类型,那么Lambda的参数部分的类型就可以省略。
函数、Lambda、闭包区别
fun 在没有等号、只有花括号的情况下,是我们常见的代码块函数体,如果返回非Unit值,必须带return;
fun 带有等号,是单表达式函数体,该情况下省略return;
如果等号加花括号的语法,那么就是一个Lambda表达式,Lambda的参数在花括号内声明。如果左侧是fun,那么就是Lambda表达式函数体,也必须通过 () 或者 invoke 来调用;
匿名函数体、Lambda在语法上都存在 {} ,由这对花括号包裹的代码块如果访问了外部环境变量则被称为一个闭包。一个闭包可以当作参数传递或者直接使用,它可以看作“访问外部环境变量的函数”
柯里化
柯里化指的是把接受多个参数的函数变换成一系列仅接受单一参数函数的过程,在返回最终结果值之前,前面的函数依次接受单个参数,然后返回下一个新的函数。
void与Void、Unit
Java中在语言层设计一个Void类。java.lang.Void类似java.lang.Integer,Void的设计是为了应对void。由于void表示没有返回值,所以Void并不能具有实例,它继承自Object。
Kotlin中引入Unit,函数式编程侧重于组合,它并不代表任何信息,用面向对象的术语来描述就是一个单例,它的实例只有一个,可写为 ()
?:
Kotlin中没有三元运算符,?: 被叫做Elvis运算符,或者null合并运算符。由于Kotlin可以用 ? 来表示类型的可控性,可以用 ?: 来给一种可空类型的便令指定为空情况下的值。
枚举
enum class Day {
MON,
TUE,
WEN,
THU,
FRI,
SAT,
SUN
}
//构造参数
enum class DayOfWeek(val day:Int) {
MON(1),
TUE(2),
WEN(3),
THU(4),
FRI(5),
SAT(6),
SUN(7)
}
when
一个完整的when类似于swith语句,由when关键字开始,用花括号包含多个分支。
每个逻辑分支都有返回值,最终整个when表达式的返回值类型就是所有分支相同的返回类型,或公共的父类型。
when关键字的参数可以省略
fun schedule(sunny: Boolean, day: Day) = when {
//...
else -> {
}
}
for
//范围表达式
for (i: Int in 1..10) {
print(i)
}
println()
//step函数定义迭代步长
for (i: Int in 1..10 step 2) {//13579
print(i)
}
println()
//倒序
for (i in 10 downTo 1 step 2) {//108642
print(i)
}
println()
//区间
for (i in 1 until 10) {//123456789
print(i)
}
中缀表达式
中缀函数必须是某个类型的扩展函数或者成员方法,只能有一个参数,参数不能有默认值,参数也不能是默认参数
public infix fun <A, B> A.to(that: B): Pair<A, B> = Pair(this, that)
//自己定义
class Person {
infix fun called(name: String) {
println("My name is $name .")
}
}
//执行
mapOf(
1 to "one",
2 to "two"
)
val person = Person()
person called "张三"
字符串
除Java中的一些字符串操作,还提供了丰富的API,由于可以扩展,所以相应的方法就很多。
==:判断两个对象的内容是否相等
===:判断两个对象的引用是否一样,与之相反的操作是 !==
如果比较的是在运行时的原始类型,=== 判断效果等于 ==
本文深入探讨Kotlin中的高阶函数与Lambda表达式,讲解其概念与应用,包括如何使用Lambda表达式重构代码,提高程序的灵活性和可读性。同时,文章还介绍了Kotlin中的闭包、柯里化、枚举类型以及一些语言特性。
2113

被折叠的 条评论
为什么被折叠?



