高阶方法Higher-Order Function
高阶方法就是他的参数,或返回值中带有方法的方法
1.实例
fun highFun(age: Int, check: (age: Int) -> Boolean) {
if (check(age)) println("This age is legal for the creature.") else println("This age is illegal for the creature.")
}
fun checkDog(age: Int): Boolean {
if (age in 0..20) return true else return false
}
fun checkPeople(age: Int): Boolean {
if (age in 0..200) return true else return false
}
//调用
highFun(100) {
checkDog(it)
}
highFun(100) {
checkPeople(it)
}
//打印
/**
This age is illegal for the creature.
This age is legal for the creature.
*/
2.方法类型
参数+返回值
如通用方法,一般方法,
带有接收器的方法
比如扩展方法,就是将class 作为接收器进行方法扩展的。
暂停方法Suspending Functions
暂时不学习 相关链接如下
https://kotlinlang.org/docs/reference/coroutines.html#suspending-functions
3.实例化方法类型
利用代码块进行实例化
- lambda 表达式
至于两种方式的异同
var lamb = { name: String, age: Int -> "My name is $name and I am $age years old." }//lambda 表达式
var nor: (name: String, age: Int) -> String = { name, age -> "My name is $name and I am $age years old." }//一般表达
println(lamb("NieBin", 28))
println(nor("Nie", 12))
- 匿名表达式
var anonym = fun(name: String, age: Int): String { return "My name is $name and I am $age years old." }
anonym("NieBin", 12)
已声明的方法或属性
class Student {
fun description(name: String, age: Int): String {
return ("What's your name?Age you $name.If you are,please tell me your age is $age.")
}
}
fun people(name: String, age: Int): String {
return ("What's your name?Age you $name.If you are,please tell me your age is $age.")
}
fun existFun(peo: (name: String, age: Int) -> String) {
println(peo("NieBin", 21))
}
//调用
existFun(::people)
var stu = Student()
existFun(stu::description)
属性和构造方法都可以通过:: 符号进行关联.
类作为接口实现的方法
class Room : (Int, Int) -> Int {
override fun invoke(p1: Int, p2: Int): Int = p1 * p2
}
fun caculate100Dimen(dimen: (Int, Int) -> Int): Int {
return dimen(100, 100)
}
//调用
var iniFunc: (Int, Int) -> Int = Room()
println(caculate100Dimen(iniFunc))
注意Room的定义方式
4.Lambda表达式
- 最后一个参数
当lambda表达式 作为最后一个参数时,可以写在括号外,如果方法没有其他参数时,括号都可以省略
fun printStu(name: String, age: Int, descrip: (String, Int) -> String): String {
var d = descrip(name, age)
return "Follow me please ! $d"
}
fun descripe(name: String, age: Int): String {
return "Yell out your name of $name and talk to me your age of $age."
}
//调用
var p = printStu("Niebin", 223) { name, age ->
descripe(name, age)
}
println(p)
- 只有一个参数
当只有一个参数时,方法自动带有隐含参数 it 来代表传入的参数
fun isThanZero(num: Int): Boolean {
if (num > 0) return true else return false
}
fun printNum(num: Int, check: (Int) -> Boolean) {
if (check(num)) {
println("This number $num is bigger than zero")
} else {
println("This number $num is not bigger than zero")
}
}
//调用 这三种方法都是可以的
printNum(1) {
isThanZero(it)
}
printNum(2) { num ->
num > 0
}
printNum(2) {
it > 0
}
- 返回lambda表达式
当lambda表达式是最后一个参数时,可以返回一个变量或常量
fun printNum(num: Int, check: (Int) -> Boolean) {
if (check(num)) {
println("This number $num is bigger than zero")
} else {
println("This number $num is not bigger than zero")
}
}
调用如下 三种方式都是可以的 前两种是返回变量,后一种直接是表达式
printNum(1) {
val z = it > 2
z
}
printNum(3) {
val z = it > 1
return@printNum z
}
printNum(10) {
it > 0
}
- 下划线变量
如果 有变量没有使用,可以用下划线代替
fun printStu(name: String, address: String, pr: (String, String) -> Unit) {
println("Let go for you name is $name and address is $address")
pr(name, address);
}
//调用
printStu("Niebin", "ShangHai") { _, addr ->
println("hhahhh")
println("This is address of $addr")
}