[英]How to call a function with a parameter in another function in kotlin
I have function which returns some value.我有 function ,它返回一些值。 In the function parameter I pass the same value:
在 function 参数中,我传递了相同的值:
fun getValue (value:String):String {
var message = value
value = "Hello"
return message
}
How can I call getValue
function in another function?如何在另一个 function 中调用
getValue
function? For example:例如:
fun getResult (){
var a = getValue (what here?)
}
You can pass a function as a prameter for another function in kotlin.您可以将 function 作为 kotlin 中另一个 function 的参数传递。 Kotlin functions can take other functions in arguments, or even return them.
Kotlin 函数可以取 arguments 中的其他函数,甚至返回它们。
fun getResult (func:(String) -> String) {
//some code
var a = func("some string")
}
fun getValue (value:String):String {
var message = value
return message
}
and call getResult and pass function to it:并调用 getResult 并将 function 传递给它:
getResult({ getValue("Hello") })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.