• Kotlin语言函数引用,内联函数,匿名函数,具名函数学习


    一、在函数中定义参数是函数的函数

    1. fun main() {
    2. loginAPI("Derry","123456") { msg : String,code : Int ->
    3. println("最终登录的情况如下:$msg,$code")
    4. }
    5. }
    6. //模拟:数据库SQLServer
    7. const val USER_NAME_SAVE_DB = "Derry"
    8. const val USER_PWD_SAVE_DB = "123456"
    9. //登录API,模仿前端
    10. public fun loginAPI(username : String,userpwd : String,responseResult : (String,Int) -> Unit) {
    11. if (username == null || userpwd == null) {
    12. TODO("用户名或密码为null") //出现问题,终止程序
    13. }
    14. //做很多的校验,前端校验
    15. if (username.length > 3 && userpwd.length >3) {
    16. if(webServiceLoginAPI(username,userpwd)) {
    17. //登录成功
    18. responseResult("login success",200)
    19. } else {
    20. //登录失败
    21. responseResult("login failed",-1)
    22. }
    23. }else {
    24. TODO("用户名和密码不合格")
    25. }
    26. }
    27. //登录API的暴露者,服务器
    28. private fun webServiceLoginAPI(name : String,pwd : String) :Boolean {
    29. //Kt的if是表达式(很灵活))
    30. return if (name == USER_NAME_SAVE_DB && pwd == USER_PWD_SAVE_DB) {
    31. true
    32. } else {
    33. flase
    34. }
    35. }

     二、Kotlin语言的函数内联学习

    1. //函数使用lambda作为参数,那么就需要声明成内联
    2. //如果此函数,不使用内联,在调用端会生成多个对象来调用(性能损耗)
    3. //如果函数使用内联,相当于C++中的#define 宏定义,宏替换会把代码替换到掉用处,调用处没有任何函数开辟,对象开辟的损耗
    4. //小结:如果函数参数有lambda,尽量使用inline关键字,这样内部会做优化,减少函数开辟,对象开辟的损耗
    5. public fun loginAPI(username : String,userpwd : String,responseResult : (Int,Int) -> Unit) {} //普通函数
    6. ​​​​​​​public inline fun loginAPI(username : String,userpwd : String,responseResult : (Int,Int) -> Unit) {} //添加内联函数关键字inline,内联函数

    三、Kotlin语言的函数引用学习

    1. fun main() {
    2. //函数引用
    3. //lambda属于函数类型的对象,需要把methodResponseResult普通函数,变成函数类型的对象
    4. login("Derry","123456",::methodResponseResult)
    5. }
    6. fun methodResponseResult(msg: String,code: Int) {
    7. println("最终登录的成果是:$msg,$code")
    8. }
    9. //模拟:数据库SQLServer
    10. const val USER_NAME_SAVE_DB = "Derry"
    11. const val USER_PWD_SAVE_DB = "123456"
    12. inline fun login(name: String,pwd:String,responseResult: (String,Int) -> Unit) {
    13. if (USER_NAME_SAVE_DB == name && USER_PWD_SAVE_DB == pwd) {
    14. //登录成功
    15. responseResult("登录成功",200)
    16. }else {
    17. //登录失败
    18. responseResult("登录失败",444)
    19. }
    20. }

    四、Kotlin函数类型作为返回类型

    1. fun main() {
    2. show("学习KT语言")
    3. val niming = show2("show") //这里会返回一个匿名函数
    4. println(niming("LU",12)) //再调用这个匿名函数
    5. }
    6. fun show(info: String) : Boolean {
    7. println("我是show函数 info:$info")
    8. return true
    9. }
    10. fun show2(info: String) : (String,Int) -> String {
    11. println("我是show函数 info:$info")
    12. //return一个函数 匿名函数
    13. return {name: String,age: Int ->
    14. "我就是匿名函数:我的name:$name,age:$age"
    15. }
    16. }

    五、Kotlin语言的匿名函数和具名函数

    1. fun main() {
    2. //匿名函数
    3. showPersonInfo("lisi",99,"男","学习KT语言"){
    4. println("显示结果:$it") //lambda表达式只有一个参数,所以可以用it
    5. }
    6. //具名函数showResultImpl
    7. showPersonInfo("wangwu",99,"女","学习JAVA语言",::showResultImpl)
    8. }
    9. fun showResultImpl(result: String) {
    10. println("显示结果:$result")
    11. }
    12. inline fun showPersonInfo(name: String,age: Int,sex: Char,study: String,showResult: (String) -> Unit) {
    13. val str = "name:$name,age:$age,sex:$sex,study:$study"
    14. showResult(str)
    15. }
    1. //Java实现匿名函数和具名函数
    2. interface IShowResult {
    3. void result(String result);
    4. }
    5. public class KtBase {
    6. public static void main(String[] args) {
    7. //匿名函数 - 匿名接口实现
    8. showPersonInfo("lisi",99,"男","study cpp",new IShowResult() {
    9. @Override
    10. public void result(String result) {
    11. System.out.println("显示结果:"+ result);
    12. }
    13. });
    14. //具名函数
    15. IShowResult showResultImpl = new MshowResultImpl();
    16. showPersonInfo("wangwu",87,"女","study JAVA",showResultImpl);
    17. }
    18. static class MshowResultImpl implements IShowResult {
    19. @Override
    20. public void result(String result) {
    21. System.out.println("显示结果:" + result);
    22. }
    23. }
    24. static void showPersonInfo(String name,int age,char sex,String study,IShowResult iShowResult) {
    25. String str = String.format("name:%s,age:%d,sex:%c,study:%s",name,age,sex,study);
    26. iShowResult.result(str)
    27. }
    28. }
  • 相关阅读:
    si9000 单端(线)&差分(动)线板层结构与阻抗计算
    不再使用步长卷积或池化:针对低分辨率图像和小物体的新的CNN构建块
    Taro中添加小程序 “lazyCodeLoading“: “requiredComponents“,
    设计模式学习笔记 - 设计原则 - 2.开闭原则
    CC0 是什么,为什么它会改变 NFT 市场?
    CE1到9关详细教程
    【Matter】解密Matter协议(二)--- 关键概念及特性
    自动驾驶学习笔记(八)——路线规划
    【微信小程序】解决分页this.setData数据量太大的限制问题
    Istio Ambient Mesh七层服务治理图文详解
  • 原文地址:https://blog.csdn.net/XXX_17/article/details/127634490