android - 如何在kotlin的另一个函数中调用带有参数的函数
问题描述
我有返回一些值的函数。在函数参数中,我传递了相同的值:
fun getValue (value:String):String {
var message = value
value = "Hello"
return message
}
如何getValue
在另一个函数中调用函数?例如:
fun getResult (){
var a = getValue (what here?)
}
解决方案
您可以将函数作为参数传递给 kotlin 中的另一个函数。Kotlin 函数可以在参数中接受其他函数,甚至返回它们。
fun getResult (func:(String) -> String) {
//some code
var a = func("some string")
}
fun getValue (value:String):String {
var message = value
return message
}
并调用 getResult 并将函数传递给它:
getResult({ getValue("Hello") })
推荐阅读
- mysql - 如果子查询在Having子句中返回null,如何处理
- html - 当文档作为 application/xhtml+xml 提供时,SVG 渲染不正确
- nuxt.js - 将 Simple Nuxt.js 应用程序部署到 now.sh 不起作用
- python - 如何使用 Tweepy 在特定日期获取推文?
- javascript - 机器人删除不是该文本通道中命令的消息
- leaflet - 如何使用 Leaflet 在两个标记之间创建折线偏移?
- python - 根据名称模式查找特定文件的代码 - break 命令的问题
- javascript - Rails 6 应用程序使用 Yarn (webpacker) 安装 Glide.js - 未捕获的 ReferenceError: Glide 未定义
- sql - 如何正确转义 SQL“LIKE”运算符的用户输入?(Postgres)
- python - 如何打印正方形的两条对角线?