当前位置:网站首页>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
}边栏推荐
- The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
- 【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
- Vulnerability recurrence fastjson deserialization
- Record a stroke skin bone error of the skirt
- The largest 3 same digits in the string of leetcode simple question
- LeetCode简单题之判断一个数的数字计数是否等于数位的值
- CDC (change data capture technology), a powerful tool for real-time database synchronization
- It took "7" years to build the robot framework into a micro service
- The element with setfieldsvalue set is obtained as undefined with GetFieldValue
- ROS bridge notes (05) - Carla_ ackermann_ Control function package (convert Ackermann messages into carlaegovehiclecontrol messages)
猜你喜欢

Leetcode simple question: find the K beauty value of a number

Register of assembly language by Wang Shuang

Myabtis_ Plus

央视太暖心了,手把手教你写HR最喜欢的简历

Leetcode medium question my schedule I

Vulnerability recurrence easy_ tornado

Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
![[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)](/img/39/cac2b5492d374da393569e2ab467a4.png)
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)

通俗易懂单点登录SSO

运放电路的反馈电阻上并联一个电容是什么作用
随机推荐
Bugku CTF daily one question chessboard with only black chess
Complete linear regression manually based on pytoch framework
BiSeNet的特点
Recursive construction of maximum binary tree
CTF-WEB shrine模板注入nmap的基本使用
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
机器人教育在动手实践中的真理
利用 Helm 在各类 Kubernetes 中安装 Rainbond
Notes on PHP penetration test topics
电池、电机技术受到很大关注,反而电控技术却很少被提及?
快解析内网穿透为文档加密行业保驾护航
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
Force buckle 144 Preorder traversal of binary tree
JS cross browser parsing XML application
Complex network modeling (III)
Network learning (II) -- Introduction to socket
Niu Mei's mathematical problem --- combinatorial number
复杂网络建模(二)
Dedecms collects content without writing rules
Summary of redis functions