当前位置:网站首页>The reified keyword in kotlin is used for generics
The reified keyword in kotlin is used for generics
2022-07-07 08:14:00 【yu-Knight】
data class ObjectClass1(val name: String, val age: Int, val study: String)
data class ObjectClass2(val name: String, val age: Int, val study: String)
data class ObjectClass3(val name: String, val age: Int, val study: String)
class KTDemo15 {
// function : Output a random object by default , If this object is inconsistent with the object specified by the user
// Return the default object, otherwise directly return this object
inline fun <reified T> randomOrDefault(defaultLambdaAction: () -> T): T? {
val objList: List<Any> = listOf(
ObjectClass1("obj1 Zhang San ", 22, " Study C"),
ObjectClass2("obj2 Li Si ", 22, " Study C++"),
ObjectClass3("obj3 Wang Wu ", 22, " Study C#")
)
val randomObj: Any? = objList.shuffled().first()
println(" The randomly generated object is :$randomObj") // The randomly generated object is :ObjectClass2(name=obj2 Li Si , age=22, study= Study C++)
// If it Randomly generated objects be equal to T type Will execute as T?
return randomObj.takeIf { it is T } as T? // null as T? Yes.
// If it Randomly generated objects It's not equal to T type , It will execute the following
?: defaultLambdaAction()
}
}
fun main(args: Array<String>) {
val finalResult = KTDemo15().randomOrDefault<ObjectClass1> {
println(" Due to the randomly generated objects and our formulation ObjectClass1 atypism , So return to the default object ")
ObjectClass1(" spare obj Wang Wu ", 33, " Study Kotlin")
}
println(" The end result at the call is :$finalResult")// The end result at the call is :ObjectClass1(name= spare obj Wang Wu , age=33, study= Study Kotlin)
}边栏推荐
- Zcmu--1492: problem d (C language)
- 饥荒云服管理脚本
- jeeSite 表单页面的Excel 导入功能
- 【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
- 【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
- DNS server configuration
- Complex network modeling (III)
- Recursive method to construct binary tree from preorder and inorder traversal sequence
- Myabtis_Plus
- Real time monitoring of dog walking and rope pulling AI recognition helps smart city
猜你喜欢

2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers

Jmeter 的使用

uniapp 移动端强制更新功能
![[quick start of Digital IC Verification] 13. SystemVerilog interface and program learning](/img/d8/ffc1b7527f0269cecb2946ab402a2e.png)
[quick start of Digital IC Verification] 13. SystemVerilog interface and program learning

Battery and motor technology have received great attention, but electric control technology is rarely mentioned?

Uniapp mobile terminal forced update function

Es FAQ summary

Network learning (III) -- highly concurrent socket programming (epoll)

Use of JMeter

青龙面板-今日头条
随机推荐
[untitled]
Blob object introduction
解读创客思维与数学课程的实际运用
Hisense TV starts the developer mode
Easy to understand SSO
面试题(CAS)
Uniapp mobile terminal forced update function
Leetcode simple question: find the K beauty value of a number
CDC (change data capture technology), a powerful tool for real-time database synchronization
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
漏洞复现-easy_tornado
船载雷达天线滑环的使用
offer收割机:两个长字符串数字相加求和(经典面试算法题)
jeeSite 表单页面的Excel 导入功能
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
在 Rainbond 中一键安装高可用 Nacos 集群
Openjudge noi 2.1 1752: chicken and rabbit in the same cage
OpenVSCode云端IDE加入Rainbond一体化开发体系
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
Excel import function of jeesite form page