当前位置:网站首页>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
}
边栏推荐
- Recursive construction of maximum binary tree
- Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
- 积分商城管理系统中应包含的四大项
- Leetcode simple question: find the K beauty value of a number
- 拓维信息使用 Rainbond 的云原生落地实践
- Recursive method to verify whether a tree is a binary search tree (BST)
- 【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
- CDC (change data capture technology), a powerful tool for real-time database synchronization
- eBPF Cilium实战(2) - 底层网络可观测性
- 【无标题】
猜你喜欢
CTF-WEB shrine模板注入nmap的基本使用
UnityHub破解&Unity破解
央视太暖心了,手把手教你写HR最喜欢的简历
快解析内网穿透为文档加密行业保驾护航
【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
发挥创客教育空间的广泛实用性
Codeforce c.strange test and acwing
The largest 3 same digits in the string of leetcode simple question
Niu Mei's mathematical problem --- combinatorial number
随机推荐
Make LIVELINK's initial pose consistent with that of the mobile capture actor
探索STEAM艺术设计中的创造力
Leetcode 187 Repeated DNA sequence (2022.07.06)
复杂网络建模(三)
Niu Mei's mathematical problem --- combinatorial number
ROS bridge notes (05) - Carla_ ackermann_ Control function package (convert Ackermann messages into carlaegovehiclecontrol messages)
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO
Openjudge noi 2.1 1752: chicken and rabbit in the same cage
LeetCode简单题之判断一个数的数字计数是否等于数位的值
利用 Helm 在各类 Kubernetes 中安装 Rainbond
Vulnerability recurrence easy_ tornado
青龙面板--整理能用脚本
Notes on PHP penetration test topics
在Rainbond中一键部署高可用 EMQX 集群
The element with setfieldsvalue set is obtained as undefined with GetFieldValue
Example of file segmentation
Offer harvester: add and sum two long string numbers (classic interview algorithm question)
Qinglong panel - today's headlines