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

2021-01-11  本文已影响0人  浩仔_Boy

函数式声明的使用,使用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")
            })
        }
上一篇下一篇

猜你喜欢

热点阅读