当前位置:网站首页>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)
}
边栏推荐
- 饥荒云服管理脚本
- 太真实了,原来自己一直没有富裕起来是有原因的
- Blob 对象介绍
- 雅思考试自己的复习进度以及方法使用【日更版】
- The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
- 【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
- ROS Bridge 笔记(05)— carla_ackermann_control 功能包(将Ackermann messages 转化为 CarlaEgoVehicleControl 消息)
- Game attack and defense world reverse
- Recursive method to verify whether a tree is a binary search tree (BST)
- Unityhub cracking & unity cracking
猜你喜欢
随机推荐
uniapp 移动端强制更新功能
The largest 3 same digits in the string of leetcode simple question
在Rainbond中实现数据库结构自动化升级
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
Network learning (III) -- highly concurrent socket programming (epoll)
Niu Mei's mathematical problem --- combinatorial number
【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
Codeforce c.strange test and acwing
eBPF Cilium实战(1) - 基于团队的网络隔离
发挥创客教育空间的广泛实用性
藏书馆App基于Rainbond实现云原生DevOps的实践
Use of JMeter
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
JS copy picture to clipboard read clipboard
Complex network modeling (III)
JS quick start (I)
eBPF Cilium实战(2) - 底层网络可观测性
ZCMU--1492: Problem D(C语言)
Bayes' law