当前位置:网站首页>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)
}
边栏推荐
- Recursive method constructs binary tree from middle order and post order traversal sequence
- Register of assembly language by Wang Shuang
- 漏洞复现-easy_tornado
- 【雅思口语】安娜口语学习记录 Part2
- Qinglong panel - today's headlines
- Bugku CTF daily one question chessboard with only black chess
- 云原生存储解决方案Rook-Ceph与Rainbond结合的实践
- 调用 pytorch API完成线性回归
- Complex network modeling (III)
- OpenVSCode云端IDE加入Rainbond一体化开发体系
猜你喜欢
BiSeNet的特点
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
JS复制图片到剪切板 读取剪切板
Network learning (I) -- basic model learning
解读创客思维与数学课程的实际运用
LeetCode简单题之字符串中最大的 3 位相同数字
Leetcode simple question: find the K beauty value of a number
Jmeter 的使用
Notes on PHP penetration test topics
Make LIVELINK's initial pose consistent with that of the mobile capture actor
随机推荐
Interactive book delivery - signed version of Oracle DBA work notes
雅思考试自己的复习进度以及方法使用【日更版】
Search for an element in a binary search tree (BST)
JS quick start (I)
Pytoch (VI) -- model tuning tricks
【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
The largest 3 same digits in the string of leetcode simple question
Openjudge noi 2.1 1752: chicken and rabbit in the same cage
CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
Pytorch(六) —— 模型调优tricks
CTF-WEB shrine模板注入nmap的基本使用
力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
Dedecms collects content without writing rules
【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
CDC (change data capture technology), a powerful tool for real-time database synchronization
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
OpenVSCode云端IDE加入Rainbond一体化开发体系
Vulnerability recurrence fastjson deserialization
解析机器人科技发展观对社会研究论