当前位置:网站首页>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)
}边栏推荐
- Merging binary trees by recursion
- Interactive book delivery - signed version of Oracle DBA work notes
- Recursive method to verify whether a tree is a binary search tree (BST)
- Dedecms collects content without writing rules
- Hisense TV starts the developer mode
- Myabtis_Plus
- 在 Rainbond 中一键安装高可用 Nacos 集群
- 海信电视开启开发者模式
- Réplication de vulnérabilité - désrialisation fastjson
- Yugu p1020 missile interception (binary search)
猜你喜欢
随机推荐
漏洞複現-Fastjson 反序列化
Zsh shell adds automatic completion and syntax highlighting
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after conne
Recursive method to verify whether a tree is a binary search tree (BST)
Complex network modeling (I)
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
Leetcode medium question my schedule I
Unityhub cracking & unity cracking
Yugu p1020 missile interception (binary search)
Complete linear regression manually based on pytoch framework
Network learning (III) -- highly concurrent socket programming (epoll)
Qinglong panel - today's headlines
Basic use of CTF web shrink template injection nmap
快解析内网穿透助力外贸管理行业应对多种挑战
Complex network modeling (II)
解析机器人科技发展观对社会研究论
Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
互动送书-《Oracle DBA工作笔记》签名版
Complex network modeling (III)
Interactive book delivery - signed version of Oracle DBA work notes







