当前位置:网站首页>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)
}
边栏推荐
- CTF-WEB shrine模板注入nmap的基本使用
- Zcmu--1492: problem d (C language)
- Introduction à l'objet blob
- LeetCode简单题之找到一个数字的 K 美丽值
- 【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
- buureservewp(2)
- Recursive method to construct binary tree from preorder and inorder traversal sequence
- Leetcode 187 Repeated DNA sequence (2022.07.06)
- 2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
- uniapp 移动端强制更新功能
猜你喜欢
CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
Réplication de vulnérabilité - désrialisation fastjson
Application of slip ring of shipborne radar antenna
Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
Vulnerability recurrence easy_ tornado
[step on the pit series] H5 cross domain problem of uniapp
JS cross browser parsing XML application
Leetcode medium question my schedule I
LeetCode简单题之找到一个数字的 K 美丽值
在Rainbond中一键部署高可用 EMQX 集群
随机推荐
Interview questions (CAS)
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
使用 Nocalhost 开发 Rainbond 上的微服务应用
The element with setfieldsvalue set is obtained as undefined with GetFieldValue
Introduction à l'objet blob
Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
JS复制图片到剪切板 读取剪切板
Network learning (II) -- Introduction to socket
Rainbond结合NeuVector实践容器安全管理
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
buureservewp(2)
LeetCode简单题之找到一个数字的 K 美丽值
运放电路的反馈电阻上并联一个电容是什么作用
Complex network modeling (III)
JS quick start (I)
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
Avatary's livedriver trial experience
海信电视开启开发者模式
Relevant data of current limiting