当前位置:网站首页>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
}
边栏推荐
- 青龙面板--整理能用脚本
- UnityHub破解&Unity破解
- 力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
- Make LIVELINK's initial pose consistent with that of the mobile capture actor
- 快解析内网穿透助力外贸管理行业应对多种挑战
- 运放电路的反馈电阻上并联一个电容是什么作用
- Jmeter 的使用
- JS copy picture to clipboard read clipboard
- Introduction à l'objet blob
- Four items that should be included in the management system of integral mall
猜你喜欢
在Rainbond中实现数据库结构自动化升级
Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
央视太暖心了,手把手教你写HR最喜欢的简历
Uniapp mobile terminal forced update function
Myabtis_ Plus
Complex network modeling (I)
机器人教育在动手实践中的真理
Dedecms collects content without writing rules
Niu Mei's mathematical problem --- combinatorial number
Vulnerability recurrence easy_ tornado
随机推荐
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
基于Pytorch 框架手动完成线性回归
Complex network modeling (II)
CDC (change data capture technology), a powerful tool for real-time database synchronization
JS copy picture to clipboard read clipboard
B. Value sequence thinking
Leetcode medium question my schedule I
Niu Mei's mathematical problem --- combinatorial number
漏洞复现-Fastjson 反序列化
Excel import function of jeesite form page
game攻防世界逆向
探索STEAM艺术设计中的创造力
LeetCode简单题之找到一个数字的 K 美丽值
Openjudge noi 2.1 1752: chicken and rabbit in the same cage
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
Record a stroke skin bone error of the skirt
【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
Qinglong panel -- Huahua reading
Recursive method to construct binary tree from preorder and inorder traversal sequence
It took "7" years to build the robot framework into a micro service