当前位置:网站首页>Use of out covariance and in inversion in kotlin
Use of out covariance and in inversion in kotlin
2022-07-07 08:14:00 【yu-Knight】
//in T out T Specify the relationship at the declaration Declared generic This is a Java No function
// Whole SetClass All the members inside Generics related , It can only be modified but not read
class SetClass<in T> {
fun set1(item: T) {
println("set1 Required item:$item")
}
fun set2(item: T) {
println("set2 Required item:$item")
}
}
// Whole getClass All the members inside Generics related , Read only, not modify
class GetClass<out T>(item: T) {
val item: T = item
fun get1(): T {
return item
}
fun get2(): T {
return item
}
}
fun main(args: Array<String>) {
// Inversion in T SetClass It can only be modified , The outside world cannot read
val p1 = SetClass<String>()
p1.set1("yuknight") //set1 Required item:yuknight
p1.set2("kotlin") //set2 Required item:kotlin
// Covariance out T GetClass Can only read , The outside world can't modify
val p2 = GetClass("yuknight")
println(p2.get1()) //yuknight
}边栏推荐
- What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
- DNS server configuration
- 王爽 《汇编语言》之寄存器
- 雅思考试自己的复习进度以及方法使用【日更版】
- Find the mode in the binary search tree (use medium order traversal as an ordered array)
- The element with setfieldsvalue set is obtained as undefined with GetFieldValue
- Openjudge noi 2.1 1752: chicken and rabbit in the same cage
- buureservewp(2)
- 运放电路的反馈电阻上并联一个电容是什么作用
- Dedecms collects content without writing rules
猜你喜欢

Force buckle 144 Preorder traversal of binary tree

解析机器人科技发展观对社会研究论

Vulnerability recurrence fastjson deserialization

CDC (change data capture technology), a powerful tool for real-time database synchronization

使用 Nocalhost 开发 Rainbond 上的微服务应用

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

What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit

拓维信息使用 Rainbond 的云原生落地实践

Bugku CTF daily one question chessboard with only black chess

提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
随机推荐
利用 Helm 在各类 Kubernetes 中安装 Rainbond
Search for an element in a binary search tree (BST)
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
Recursive method constructs binary tree from middle order and post order traversal sequence
Myabtis_ Plus
Offer harvester: add and sum two long string numbers (classic interview algorithm question)
面试题(CAS)
解读创客思维与数学课程的实际运用
互动送书-《Oracle DBA工作笔记》签名版
快解析内网穿透助力外贸管理行业应对多种挑战
Real time monitoring of dog walking and rope pulling AI recognition helps smart city
海信电视开启开发者模式
Roulette chart 2 - writing of roulette chart code
JS copy picture to clipboard read clipboard
电池、电机技术受到很大关注,反而电控技术却很少被提及?
饥荒云服管理脚本
贝叶斯定律
Myabtis_Plus
Complex network modeling (II)
Interview questions (CAS)