当前位置:网站首页>The use of generics and vararg variable parameters in kotlin
The use of generics and vararg variable parameters in kotlin
2022-07-07 08:14:00 【yu-Knight】
//Kotlin Medium generics and vararg The use of variable parameters
class KtBase10<T>(vararg objects: T, var isMap: Boolean) {
//out there T Can only be read , Do not modify
private val objectArray: Array<out T> = objects
fun showObj(index: Int): T? = objectArray[index].takeIf { isMap } ?: null
fun <O> mapObj(index: Int, mapAction: (T?) -> O): O? = mapAction(objectArray[index].takeIf { isMap })
// fun <O> mapObj(index: Int, mapAction: (T?) -> O): O? {
// return mapAction(objectArray[index].takeIf { isMap })
// }
}
fun main(args: Array<String>) {
val p: KtBase10<Any?> = KtBase10("yuknight", 123, 22.1f, null, 'A', isMap = true)
println(p.showObj(0))
println(p.showObj(1))
println(p.showObj(2))
println(p.showObj(3))
println(p.showObj(4))
println()
var r: Any? = p.mapObj(0) {
it.toString().length
}
println(r)//8
var r1 = p.mapObj(1) { obj: Any? ->
// obj.toString().length
" The second parameter is :$obj"
}
println(r1)// The second parameter is :123
//
val p2: KtBase10<String> = KtBase10("AAA", "BBB", "CCCC", isMap = true)
val r3 = p2.mapObj(0){
it
" Turn into String type :$it"
}
println(r3)// Turn into String type :AAA
val r4 = p2.mapObj(2){
str: String? -> " Turn into String type :$str"
}
println(r4)// Turn into String type :CCCC
}边栏推荐
- Lua 编程学习笔记
- Find the mode in the binary search tree (use medium order traversal as an ordered array)
- 青龙面板-今日头条
- jeeSite 表单页面的Excel 导入功能
- JS quick start (I)
- It took "7" years to build the robot framework into a micro service
- buureservewp(2)
- 太真实了,原来自己一直没有富裕起来是有原因的
- Qinglong panel - today's headlines
- ZCMU--1396: 队列问题(2)
猜你喜欢

饥荒云服管理脚本

Network learning (I) -- basic model learning

buureservewp(2)

船载雷达天线滑环的使用

Fast parsing intranet penetration escorts the document encryption industry

eBPF Cilium实战(2) - 底层网络可观测性

Unityhub cracking & unity cracking

LeetCode中等题之我的日程安排表 I

Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验

The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
随机推荐
QT learning 26 integrated example of layout management
饥荒云服管理脚本
Merging binary trees by recursion
Myabtis_ Plus
Call pytorch API to complete linear regression
ROS Bridge 笔记(05)— carla_ackermann_control 功能包(将Ackermann messages 转化为 CarlaEgoVehicleControl 消息)
jeeSite 表单页面的Excel 导入功能
复杂网络建模(二)
ZCMU--1396: 队列问题(2)
积分商城管理系统中应包含的四大项
Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
JS quick start (I)
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
青龙面板-今日头条
It took "7" years to build the robot framework into a micro service
Complex network modeling (III)
Basic use of CTF web shrink template injection nmap
LeetCode简单题之找到一个数字的 K 美丽值
Lua 编程学习笔记
Relevant data of current limiting