函数式声明的使用,使用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")
})
}
网友评论