当前位置:网站首页>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)
}
边栏推荐
- 积分商城管理系统中应包含的四大项
- Complex network modeling (III)
- Blob 對象介紹
- The element with setfieldsvalue set is obtained as undefined with GetFieldValue
- Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
- Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)
- Introduction à l'objet blob
- Blob object introduction
- 使用 Nocalhost 开发 Rainbond 上的微服务应用
- [quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
猜你喜欢
快解析内网穿透为文档加密行业保驾护航
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
在Rainbond中一键部署高可用 EMQX 集群
Notes on PHP penetration test topics
BiSeNet的特点
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
The largest 3 same digits in the string of leetcode simple question
The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
JS copy picture to clipboard read clipboard
随机推荐
使用 Nocalhost 开发 Rainbond 上的微服务应用
复杂网络建模(一)
海信电视开启开发者模式
Zsh shell adds automatic completion and syntax highlighting
解析创新教育体系中的创客教育
Excel import function of jeesite form page
Topic not received? Try this
Complex network modeling (III)
Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
B. Value sequence thinking
ROS bridge notes (05) - Carla_ ackermann_ Control function package (convert Ackermann messages into carlaegovehiclecontrol messages)
Qinglong panel -- finishing usable scripts
面试题(CAS)
Introduction à l'objet blob
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
Rainbond结合NeuVector实践容器安全管理
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
央视太暖心了,手把手教你写HR最喜欢的简历
Myabtis_Plus