你如何在 Kotlin 中定義一組 funs(functions)?
這是我對如何完成的假設......
this.funs = arrayOf : Unit(
this::openCamera,
this::sendSMS,
this::makeCall
)
如何在運行時將每個陣列項作為函式呼叫?在 forEach() 回圈中簡單地指定 'funs' 代替 openCamera()、sendSMS() 和 MakeCall() 是否可行?
或者..這是我呼叫每個函式的方式:
val function = getFunction(funs)
function()
uj5u.com熱心網友回復:
如果你有一些函式定義為
fun one() { print("ONE") }
fun two() { print("TWO") }
fun three() { print("THREE") }
然后你可以創建這些陣列
fun someFun(){
val arrayOfFunctions: Array<() -> Unit> = arrayOf(::one, ::two, ::three)
arrayOfFunctions.forEach { function ->
function()
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/324547.html
