当前位置:网站首页>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
}
边栏推荐
- Codeforce c.strange test and acwing
- Four items that should be included in the management system of integral mall
- Zsh shell adds automatic completion and syntax highlighting
- Find the mode in the binary search tree (use medium order traversal as an ordered array)
- buureservewp(2)
- Easy to understand SSO
- The legend about reading the configuration file under SRC
- Register of assembly language by Wang Shuang
- LeetCode简单题之判断一个数的数字计数是否等于数位的值
- BiSeNet的特点
猜你喜欢
The largest 3 same digits in the string of leetcode simple question
Network learning (II) -- Introduction to socket
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
Leetcode simple question: find the K beauty value of a number
JS quick start (I)
JS cross browser parsing XML application
漏洞复现-Fastjson 反序列化
快解析内网穿透助力外贸管理行业应对多种挑战
Record a stroke skin bone error of the skirt
随机推荐
让Livelink初始Pose与动捕演员一致
Qinglong panel -- Huahua reading
Example of file segmentation
雅思考试自己的复习进度以及方法使用【日更版】
Yugu p1020 missile interception (binary search)
Register of assembly language by Wang Shuang
Es FAQ summary
调用 pytorch API完成线性回归
电池、电机技术受到很大关注,反而电控技术却很少被提及?
Force buckle 144 Preorder traversal of binary tree
B. Value sequence thinking
[step on the pit series] H5 cross domain problem of uniapp
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
eBPF Cilium实战(1) - 基于团队的网络隔离
CTF-WEB shrine模板注入nmap的基本使用
Application of slip ring of shipborne radar antenna
贝叶斯定律
ZCMU--1396: 队列问题(2)
Codeforce c.strange test and acwing
Rainbond 5.7.1 支持对接多家公有云和集群异常报警