Kotlin 高阶函数概念 VS forEach 方法参数解析

2021-03-0514:39:45编程语言入门到精通Comments2,179 views字数 1650阅读模式

I . Kotlin 高阶函数概念


Kotlin 高阶函数 : Kotlin 的高阶函数 , 就是方法的参数 或 返回值 是函数类型的 函数 ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

如 :List 集合的 forEach( ) 循环 , 该方法就是接收一个高阶函数类型变量作为参数 , 有点类似于 C/C++ 中的 函数指针 ( 指向函数的指针 ) ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

II . Kotlin 高阶函数 作为 参数 示例解析 ( forEach 方法参数解析 )


1 . 高阶函数引入 :List 集合的 forEach 方法的参数 , 就是一个高阶函数 ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

2 . forEach () 函数原型 :forEach() 遍历集合的方法需要传入一个参数 , 下面解析这个参数 :文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

@kotlin.internal.HidesMembers
publicinlinefun<T> Iterable<T>.forEach(action:(T)-> Unit): Unit {for(element inthis)action(element)}

3 . 参数类型分析 :由上面的函数原型可以看到 , forEach ( ) 方法的参数是 action: (T) -> Unit , 参数类型是 (T) -> Unit 类型 , 参数变量名称是 action ; 这个参数类型 (T) -> Unit 类型 就是高阶函数类型 ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

4 . forEach(action: (T) -> Unit) 参数解析 :文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

① 参数名 :action ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

② 参数类型 :(T) -> Unit ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

③ T 类型 :T 表示 forEach 遍历的集合元素类型 , 这里是 String 类型 ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

5 . (String) -> Unit 函数类型解析 :文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

① 本质 :是函数类型 ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

② 函数参数 :其函数类型的参数是 String 字符串类型文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

③ 函数返回值 :其函数类型的返回值类型是 Unit 类型 , 这是空返回值 相当于 Java 中的 void 类型 ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

III . 用作函数参数的 函数类型变量 ( 本示例中使用匿名函数 )


1 . 函数变量需求 :在上面的 forEach ( ) 函数中 , 需要传入一个 (String) -> Unit 函数类型的变量 , 该函数类型的函数 参数是 String 类型 , 返回值是 Unit 空类型 ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

2 . 普通的函数声明 :下面定义的函数 , 参数类型是 String , 返回值是 Unit 空类型 , 这个函数是 (String) -> Unit 类型的 , 但是 study 不能当做参数传入到 forEach 方法中 ; (study) , 是错误的调用 , 编译不通过 ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

funstudy(student : String): Unit{println(student +" 在学习")}

3 . 函数类型变量 :可以使用匿名函数 , 赋值给一个变量 , 然后将这个变量当做参数传递给 forEach 当做参数 ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

① 指定变量 :为 (String) -> Unit 类型函数指定一个引用变量 var study2 ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

② 匿名函数 :顾名思义 , 就是没有函数名称 , 省略调上面普通函数的名称 , 赋值给变量 ; 具体用法如下 :文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

var study2 =fun(student : String): Unit{println(student +" 在学习")}

IV . 函数类型实例使用


1 . 函数类型实例使用 :上面的 study2 就是 (String) -> Unit 函数类型的实例变量 ;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

var study2 =fun(student : String): Unit{println(student +" 在学习")}var students : List<String>= listOf<String>("Tom","Jerry")
students.forEach(study2);

2 . 执行结果 :文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

Tom 在学习
Jerry 在学习

V . Kotlin 高阶函数 整体代码示例


Kotlin 高阶函数 整体代码示例 :文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

package fp

funstudy(student : String): Unit{println(student +" 在学习")}var study2 =fun(student : String): Unit{println(student +" 在学习")}funmain(){var students : List<String>= listOf<String>("Tom","Jerry")
    students.forEach(study2);}

执行结果 :文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html

Tom 在学习
Jerry 在学习
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/21036.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/21036.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定