当前位置:网站首页>kotlin中函数作为参数和函数作为返回值实例练习
kotlin中函数作为参数和函数作为返回值实例练习
2022-07-31 01:17:00 【汤米粥】
kotlin中函数作为参数和函数作为返回值,在工作中写代码有时会被卡住,怎么写都提示语法错误,今天专门研究一下几种常用的用法。
package com.example.test
import android.util.Log
import java.util.*
class KotlinSample {
//直接定义函数
//带参数,没有返回值的函数
private var myPrint: (msg: String) -> Unit = { msg -> Log.e("xxx5",msg) }
//带参数,有返回值的函数
private var hello : (msg: String) -> String = { "hello ${it.uppercase()}!!" }
//函数作为参数
fun callMethods() {
method1 {
var hello = "hello world"
Log.e("xxx1", hello)
hello
}
method2 {
var hello = "hello world"
Log.e("xxx2", hello)
}
method3("hello world") { msg ->
Log.e("xxx3", msg)
}
//函数作为返回值
var method4 = method4("hello world")
method4.invoke()
//使用定义的函数
myPrint.invoke("hello world")
var greet = hello("zhang san")
myPrint.invoke(greet)
}
//返回String 但是不需要return 直接将字符串写在最后一行
private fun method1(method: () -> String) {
method.invoke()
}
//Unit表示没有返回值
private fun method2(method: () -> Unit) {
method.invoke()
}
//函数作为参数,有一个输入参数时。不能直接带给它,需要另外一个参数传进来。
private fun <T> method3(msg1: T, method: (msg: T) -> Unit) {
method.invoke(msg1)
}
//函数作为返回值
private fun method4(str: String): () -> Unit {
Log.e("xxx4", "这一部分不返回,直接运行")
return {
var strNew = str.uppercase(Locale.getDefault())
Log.e("xxx4", strNew)
}
}
}
运行结果:
2022-07-28 11:05:04.308 20589-20589/com.example.test E/xxx1: hello world
2022-07-28 11:05:04.308 20589-20589/com.example.test E/xxx2: hello world
2022-07-28 11:05:04.308 20589-20589/com.example.test E/xxx3: hello world
2022-07-28 11:05:04.308 20589-20589/com.example.test E/xxx4: 这一部分不返回,直接运行
2022-07-28 11:05:04.308 20589-20589/com.example.test E/xxx4: HELLO WORLD
2022-07-28 11:05:04.309 20589-20589/com.example.test E/xxx5: hello world
2022-07-28 11:05:04.309 20589-20589/com.example.test E/xxx5: hello ZHANG SAN!!
边栏推荐
- How to Add a Navigation Menu on Your WordPress Site
- typescript11 - data types
- 【genius_platform软件平台开发】第七十四讲:window环境下的静态库和动态库的一些使用方法(VC环境)
- ROS Action通信
- ShardingSphere's vertical sub-database sub-table actual combat (5)
- DOM系列之 client 系列
- 35. Reverse linked list
- ShardingSphere read-write separation (8)
- Huawei's "genius boy" Zhihui Jun has made a new work, creating a "customized" smart keyboard from scratch
- typescript9-常用基础类型
猜你喜欢
typescript17 - function optional parameters
网站频繁出现mysql等数据库连接失败等信息解决办法
蓝牙mesh系统开发二 mesh节点开发
ShardingSphere's public table combat (7)
ShardingSphere's unsharded table configuration combat (6)
ShardingSphere之未分片表配置实战(六)
The difference between 4G communication module CAT1 and CAT4
ShardingSphere之公共表实战(七)
Summary of MySQL database interview questions (2022 latest version)
ShardingSphere之垂直分库分表实战(五)
随机推荐
typescript10-常用基础类型
typescript11-数据类型
Mini Program - Global Data Sharing
Zabbix干啥用?
ShardingSphere's public table combat (7)
typescript11 - data types
【Mysql】——索引的深度理解
【952. Calculate the maximum component size according to the common factor】
Basic Parameters of RF Devices 1
Summary of MySQL database interview questions (2022 latest version)
Sping.事务的传播特性
Xiaohei's leetcode journey: 117. Fill the next right node pointer of each node II
822. 走方格
小黑leetcode之旅:104. 二叉树的最大深度
ShardingSphere之未分片表配置实战(六)
小程序-全局数据共享
《实战》基于情感词典的文本情感分析与LDA主题分析
分布式.分布式锁
软件测试工作3年了,谈谈我是如何从刚入门进阶到自动化测试的?
Jetpack Compose learning (8) - State and remeber