美文网首页
关于函数式声明的简单使用

关于函数式声明的简单使用

作者: 浩仔_Boy | 来源:发表于2021-01-11 17:37 被阅读0次

    函数式声明的使用,使用inline函数的原因是:函数类型在调用的时候会生成一个对象,频繁调用会增大内存的开销

            private inline fun loginApi(
                context: Context,
                success: () -> Unit,
                fail: (errorCode: Int, errorMessage: String) -> Unit
            ) {
                val r = Random.nextInt()
                if (r < 10) {
                    success()
                } else {
                    fail(r, "失败了")
                }
            }
    
            fun testLoginApi() {
                loginApi(appContext, success = {
                    Log.d("success","success")
                }, fail = { errorCode, errorMessage ->
                    Log.d("fail", "错误码:$errorCode  错误信息:$errorMessage")
                })
            }
    

    相关文章

      网友评论

          本文标题:关于函数式声明的简单使用

          本文链接:https://www.haomeiwen.com/subject/pxfdaktx.html