当前位置:网站首页>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)
}边栏推荐
- The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
- WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after conne
- 使用 Nocalhost 开发 Rainbond 上的微服务应用
- Network learning (III) -- highly concurrent socket programming (epoll)
- Recursive construction of maximum binary tree
- Introduction à l'objet blob
- 【无标题】
- Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)
- Fast parsing intranet penetration escorts the document encryption industry
- 雅思考试自己的复习进度以及方法使用【日更版】
猜你喜欢

藏书馆App基于Rainbond实现云原生DevOps的实践

在Rainbond中实现数据库结构自动化升级

让Livelink初始Pose与动捕演员一致

在 Rainbond 中一键安装高可用 Nacos 集群

一文了解如何源码编译Rainbond基础组件

LeetCode简单题之字符串中最大的 3 位相同数字

Bugku CTF daily one question chessboard with only black chess

Rainbond结合NeuVector实践容器安全管理

uniapp 移动端强制更新功能
![[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO](/img/56/82f4533b5bded73df222ef65101a72.png)
[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO
随机推荐
Example of file segmentation
CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
Zcmu--1492: problem d (C language)
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
Avatary's livedriver trial experience
game攻防世界逆向
ZCMU--1396: 队列问题(2)
Leetcode simple question: find the K beauty value of a number
Blob object introduction
Summary of redis functions
Excel import function of jeesite form page
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
央视太暖心了,手把手教你写HR最喜欢的简历
Réplication de vulnérabilité - désrialisation fastjson
uniapp 移动端强制更新功能
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
Recursive method constructs binary tree from middle order and post order traversal sequence
Qinglong panel -- finishing usable scripts
柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户