当前位置:网站首页>Understanding of out covariance, in inversion and invariance in kotlin
Understanding of out covariance, in inversion and invariance in kotlin
2022-07-07 08:14:00 【yu-Knight】
java in ? extends T amount to kotlin Medium out String yes CharSequence Subclasses of List<CharSequence> list = new ArrayList<CharSequence>(); List<? extends CharSequence> list2 = new ArrayList<String>(); // out T Generic concrete subclass objects can be assigned to the parent object at the generic declaration ------------------------------------------------------------- java in ? super T amount to kotlin Medium in List<? super String> list3 = new ArrayList<CharSequence>(); The parent object of the generic concrete , Cannot be assigned to Subclasses at the generic declaration
// producer out T Covariance [out T This generic type can be obtained Read So it is out]
interface Producer<out T> {
// out T Represents the whole production class This T Can only be read , Do not modify
// Cannot be modified ( Compile not pass )
// fun consumer(item: T)
// Can only be read
fun producer(): T
}
// consumer in T Inversion [in T This generic can only be modified to update So it is in]
interface Consumer<in T> {
// in T Represents the whole consumer category This T Can only be modified , Cannot read
// Can only be modified
fun consumer(item: T)
// Can't be read
// fun producer():T
}
// producers and consumers T By default , unchanged
interface ProducerAndConsumer<T> {
// Can be modified
fun consumer(item: T)
// Can be read
fun producer(): T
}
open class Animal // animal
open class Humanity : Animal() // human beings
open class Man : Humanity()// Man
open class WoMan : Humanity()// A woman
class ProducerClass1 : Producer<Animal> {
override fun producer(): Animal {
println(" producer Animal")
return Animal()
}
}
class ProducerClass2 : Producer<Humanity> {
override fun producer(): Humanity {
println(" producer Humanity")
return Humanity()
}
}
class ProducerClass3 : Producer<Man> {
override fun producer(): Man {
println(" producer Man")
return Man()
}
}
class ProducerClass4 : Producer<WoMan> {
override fun producer(): WoMan {
println(" producer WoMan")
return WoMan()
}
}
/******************************************************/
class ConsumerClass1:Consumer<Animal>{
override fun consumer(item: Animal) {
println(" consumer Animal")
}
}
class ConsumerClass2:Consumer<Humanity>{
override fun consumer(item: Humanity) {
println(" consumer Humanity")
}
}
class ConsumerClass3:Consumer<Man>{
override fun consumer(item: Man) {
println(" consumer Man")
}
}
class ConsumerClass4:Consumer<WoMan>{
override fun consumer(item: WoMan) {
println(" consumer WoMan")
}
}
fun main(args: Array<String>) {
val p1: Producer<Animal> = ProducerClass1()//ProducerClass1 It's delivery Animal
val p2: Producer<Animal> = ProducerClass2()//ProducerClass2 It was originally a transmission Humanity Yes out Modifiers can be turned into parent classes
val p3: Producer<Animal> = ProducerClass3()//ProducerClass3 It was originally a transmission Man Yes out Modifiers can be turned into parent classes
val p4: Producer<Animal> = ProducerClass4()//ProducerClass4 It was originally a transmission WoMan Yes out Modifiers can be turned into parent classes
// By default, generics are : Subclass objects of generics , Cannot assign to a generic parent object
// By default, generics are : Subclass objects in the specific place of the generic , Cannot assign to the parent object at the generic declaration
//out: Subclass objects of generics Can be assigned to generic parent objects
//out: Generic concrete subclass objects can be assigned to the parent object at the generic declaration
// Covariance : Parent class Generic declaration Office You can receive Subclass generic concrete object
/*********************************************/
val c1:Consumer<Man> = ConsumerClass1()//ConsumerClass Originally, it was Animal, Yes in Modification can be converted into subclasses
val c2:Consumer<WoMan> = ConsumerClass1()//ConsumerClass Originally, it was Animal, Yes in Modification can be converted into subclasses
// By default : The parent object of the generic concrete , Cannot be assigned to Subclasses at the generic declaration
//in: The parent class of the generic concrete , Is a subclass that can be assigned to a generic declaration
//---------------------------------------------//
// Covariance : out Parent class = Subclass
// Inversion : in Subclass = Parent class
}
边栏推荐
- 使用 Nocalhost 开发 Rainbond 上的微服务应用
- OpenJudge NOI 2.1 1752:鸡兔同笼
- 柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
- 【雅思口语】安娜口语学习记录 Part2
- Register of assembly language by Wang Shuang
- Find the mode in the binary search tree (use medium order traversal as an ordered array)
- Record a stroke skin bone error of the skirt
- eBPF Cilium实战(2) - 底层网络可观测性
- WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after conne
- 在 Rainbond 中一键安装高可用 Nacos 集群
猜你喜欢
BiSeNet的特点
buureservewp(2)
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
buureservewp(2)
CTF-WEB shrine模板注入nmap的基本使用
机器人教育在动手实践中的真理
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
Leetcode medium question my schedule I
eBPF Cilium实战(1) - 基于团队的网络隔离
随机推荐
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
解读创客思维与数学课程的实际运用
Fast parsing intranet penetration escorts the document encryption industry
The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
OpenVSCode云端IDE加入Rainbond一体化开发体系
uniapp 移动端强制更新功能
buureservewp(2)
Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
Easy to understand SSO
Introduction à l'objet blob
CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
Complete linear regression manually based on pytoch framework
贝叶斯定律
解析机器人科技发展观对社会研究论
The largest 3 same digits in the string of leetcode simple question
Find the mode in the binary search tree (use medium order traversal as an ordered array)
饥荒云服管理脚本
Record a stroke skin bone error of the skirt
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
Zsh shell adds automatic completion and syntax highlighting