一、在函数中定义参数是函数的函数
- fun main() {
- loginAPI("Derry","123456") { msg : String,code : Int ->
- println("最终登录的情况如下:$msg,$code")
- }
- }
-
- //模拟:数据库SQLServer
- const val USER_NAME_SAVE_DB = "Derry"
- const val USER_PWD_SAVE_DB = "123456"
-
- //登录API,模仿前端
- public fun loginAPI(username : String,userpwd : String,responseResult : (String,Int) -> Unit) {
- if (username == null || userpwd == null) {
- TODO("用户名或密码为null") //出现问题,终止程序
- }
- //做很多的校验,前端校验
- if (username.length > 3 && userpwd.length >3) {
- if(webServiceLoginAPI(username,userpwd)) {
- //登录成功
- responseResult("login success",200)
- } else {
- //登录失败
- responseResult("login failed",-1)
- }
- }else {
- TODO("用户名和密码不合格")
- }
- }
-
- //登录API的暴露者,服务器
- private fun webServiceLoginAPI(name : String,pwd : String) :Boolean {
- //Kt的if是表达式(很灵活))
- return if (name == USER_NAME_SAVE_DB && pwd == USER_PWD_SAVE_DB) {
- true
- } else {
- flase
- }
- }
二、Kotlin语言的函数内联学习
- //函数使用lambda作为参数,那么就需要声明成内联
- //如果此函数,不使用内联,在调用端会生成多个对象来调用(性能损耗)
- //如果函数使用内联,相当于C++中的#define 宏定义,宏替换会把代码替换到掉用处,调用处没有任何函数开辟,对象开辟的损耗
- //小结:如果函数参数有lambda,尽量使用inline关键字,这样内部会做优化,减少函数开辟,对象开辟的损耗
- public fun loginAPI(username : String,userpwd : String,responseResult : (Int,Int) -> Unit) {} //普通函数
-
- public inline fun loginAPI(username : String,userpwd : String,responseResult : (Int,Int) -> Unit) {} //添加内联函数关键字inline,内联函数
三、Kotlin语言的函数引用学习
- fun main() {
- //函数引用
- //lambda属于函数类型的对象,需要把methodResponseResult普通函数,变成函数类型的对象
- login("Derry","123456",::methodResponseResult)
- }
-
- fun methodResponseResult(msg: String,code: Int) {
- println("最终登录的成果是:$msg,$code")
- }
-
- //模拟:数据库SQLServer
- const val USER_NAME_SAVE_DB = "Derry"
- const val USER_PWD_SAVE_DB = "123456"
- inline fun login(name: String,pwd:String,responseResult: (String,Int) -> Unit) {
- if (USER_NAME_SAVE_DB == name && USER_PWD_SAVE_DB == pwd) {
- //登录成功
- responseResult("登录成功",200)
- }else {
- //登录失败
- responseResult("登录失败",444)
- }
- }
四、Kotlin函数类型作为返回类型
- fun main() {
- show("学习KT语言")
- val niming = show2("show") //这里会返回一个匿名函数
- println(niming("LU",12)) //再调用这个匿名函数
- }
-
- fun show(info: String) : Boolean {
- println("我是show函数 info:$info")
- return true
- }
-
- fun show2(info: String) : (String,Int) -> String {
- println("我是show函数 info:$info")
- //return一个函数 匿名函数
- return {name: String,age: Int ->
- "我就是匿名函数:我的name:$name,age:$age"
- }
- }
五、Kotlin语言的匿名函数和具名函数
- fun main() {
- //匿名函数
- showPersonInfo("lisi",99,"男","学习KT语言"){
- println("显示结果:$it") //lambda表达式只有一个参数,所以可以用it
- }
- //具名函数showResultImpl
- showPersonInfo("wangwu",99,"女","学习JAVA语言",::showResultImpl)
- }
- fun showResultImpl(result: String) {
- println("显示结果:$result")
- }
-
- inline fun showPersonInfo(name: String,age: Int,sex: Char,study: String,showResult: (String) -> Unit) {
- val str = "name:$name,age:$age,sex:$sex,study:$study"
- showResult(str)
- }
- //Java实现匿名函数和具名函数
- interface IShowResult {
- void result(String result);
- }
-
- public class KtBase {
- public static void main(String[] args) {
- //匿名函数 - 匿名接口实现
- showPersonInfo("lisi",99,"男","study cpp",new IShowResult() {
- @Override
- public void result(String result) {
- System.out.println("显示结果:"+ result);
- }
- });
-
- //具名函数
- IShowResult showResultImpl = new MshowResultImpl();
- showPersonInfo("wangwu",87,"女","study JAVA",showResultImpl);
- }
-
- static class MshowResultImpl implements IShowResult {
- @Override
- public void result(String result) {
- System.out.println("显示结果:" + result);
- }
- }
-
- static void showPersonInfo(String name,int age,char sex,String study,IShowResult iShowResult) {
- String str = String.format("name:%s,age:%d,sex:%c,study:%s",name,age,sex,study);
- iShowResult.result(str)
- }
- }