当前位置:网站首页>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
}
边栏推荐
- Rainbond 5.7.1 支持对接多家公有云和集群异常报警
- 王爽 《汇编语言》之寄存器
- ZCMU--1396: 队列问题(2)
- 央视太暖心了,手把手教你写HR最喜欢的简历
- Four items that should be included in the management system of integral mall
- Call pytorch API to complete linear regression
- 太真实了,原来自己一直没有富裕起来是有原因的
- Uniapp mobile terminal forced update function
- buureservewp(2)
- Notes on PHP penetration test topics
猜你喜欢
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
发挥创客教育空间的广泛实用性
jeeSite 表单页面的Excel 导入功能
Make LIVELINK's initial pose consistent with that of the mobile capture actor
The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
buureservewp(2)
Yugu p1020 missile interception (binary search)
Fast parsing intranet penetration escorts the document encryption industry
Leetcode medium question my schedule I
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
随机推荐
Es FAQ summary
运放电路的反馈电阻上并联一个电容是什么作用
电池、电机技术受到很大关注,反而电控技术却很少被提及?
OpenJudge NOI 2.1 1752:鸡兔同笼
发挥创客教育空间的广泛实用性
Use of JMeter
【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
LeetCode中等题之我的日程安排表 I
LeetCode简单题之判断一个数的数字计数是否等于数位的值
Zcmu--1396: queue problem (2)
Recursive method constructs binary tree from middle order and post order traversal sequence
复杂网络建模(二)
[step on the pit series] H5 cross domain problem of uniapp
game攻防世界逆向
Dedecms collects content without writing rules
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
Roulette chart 2 - writing of roulette chart code
Zsh shell adds automatic completion and syntax highlighting
LeetCode简单题之字符串中最大的 3 位相同数字
复杂网络建模(一)