当前位置:网站首页>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
}
边栏推荐
- Force buckle 144 Preorder traversal of binary tree
- Vulnerability recurrence easy_ tornado
- 【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
- 柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
- eBPF Cilium实战(1) - 基于团队的网络隔离
- Myabtis_ Plus
- 复杂网络建模(三)
- The legend about reading the configuration file under SRC
- Linux Installation MySQL 8.0 configuration
- Recursive method to verify whether a tree is a binary search tree (BST)
猜你喜欢
CTF-WEB shrine模板注入nmap的基本使用
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
藏书馆App基于Rainbond实现云原生DevOps的实践
UnityHub破解&Unity破解
JS cross browser parsing XML application
发挥创客教育空间的广泛实用性
LeetCode简单题之找到一个数字的 K 美丽值
eBPF Cilium实战(1) - 基于团队的网络隔离
Record a stroke skin bone error of the skirt
LeetCode中等题之我的日程安排表 I
随机推荐
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
eBPF Cilium实战(1) - 基于团队的网络隔离
Zcmu--1396: queue problem (2)
贝叶斯定律
UnityHub破解&Unity破解
电池、电机技术受到很大关注,反而电控技术却很少被提及?
ZCMU--1396: 队列问题(2)
藏书馆App基于Rainbond实现云原生DevOps的实践
DNS server configuration
追风赶月莫停留,平芜尽处是春山
【雅思口语】安娜口语学习记录 Part3
Record a stroke skin bone error of the skirt
The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
Use of JMeter
CDC (change data capture technology), a powerful tool for real-time database synchronization
积分商城管理系统中应包含的四大项
Bayes' law
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
The legend about reading the configuration file under SRC
OpenJudge NOI 2.1 1752:鸡兔同笼