简体   繁体   English

如何在 kotlin 中使用另一个 function 中的参数调用 function

[英]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.

相关问题 如何在Java代码中以函数作为参数调用Kotlin函数? - How to call Kotlin function with function as parameter in Java Code? 如何调用作为另一个 function 结果的特定命名参数? Kotlin - How to call specific named argument which is a result of another function? Kotlin 从另一个类\函数调用片段 kotlin - call a fragment from another class\function kotlin 当JsonObjectRequest返回成功时,Android Kotlin如何将命名函数作为参数传递给另一个要执行的函数? - Android Kotlin how to pass named function as parameter into another function to be executed when JsonObjectRequest return success? Android Kotlin - 如何将命名的 function 传递给另一个 ZC19C4252674C17A 中的 DialogInterface.OnClickListener 参数 - Android Kotlin - How to pass a named function to DialogInterface.OnClickListener parameter in another function? 如何在Android的另一个类中调用带有View类型参数的函数 - How to call a function with a View type parameter in another class in android 如何将 class 实例作为参数提供给 Kotlin Function - How to supply class instance as a parameter to Kotlin Function 如何在 kotlin 中将函数作为参数传递 - Android - How to pass a function as parameter in kotlin - Android 如何将参数传递给 Kotlin 中的扩展 function - How to pass a parameter to a extension function in Kotlin Kotlin 调用 function 参数使用 Any is xxx || 任何 yyy 都不起作用 - Kotlin call function with parameter by using Any is xxx || Any is yyy not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM