当前位置:网站首页>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
}边栏推荐
- Easy to understand SSO
- Force buckle 144 Preorder traversal of binary tree
- 云原生存储解决方案Rook-Ceph与Rainbond结合的实践
- Network learning (III) -- highly concurrent socket programming (epoll)
- JS quick start (I)
- 力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
- 解读创客思维与数学课程的实际运用
- OpenVSCode云端IDE加入Rainbond一体化开发体系
- Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
- 漏洞复现-Fastjson 反序列化
猜你喜欢

Fast parsing intranet penetration escorts the document encryption industry

Complex network modeling (I)

【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)

Avatary's livedriver trial experience

漏洞复现-Fastjson 反序列化

【雅思口语】安娜口语学习记录 Part2

LeetCode简单题之字符串中最大的 3 位相同数字

The largest 3 same digits in the string of leetcode simple question
![[step on the pit series] H5 cross domain problem of uniapp](/img/53/bd836a5c5545f51be929d8d123b961.png)
[step on the pit series] H5 cross domain problem of uniapp
![[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)](/img/e1/9a047ef13299b94b5314ee6865ba26.png)
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
随机推荐
Topic not received? Try this
青龙面板--花花阅读
Complex network modeling (I)
The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
Openjudge noi 2.1 1752: chicken and rabbit in the same cage
Search for an element in a binary search tree (BST)
雅思考试自己的复习进度以及方法使用【日更版】
ROS Bridge 笔记(05)— carla_ackermann_control 功能包(将Ackermann messages 转化为 CarlaEgoVehicleControl 消息)
It took "7" years to build the robot framework into a micro service
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
Uniapp mobile terminal forced update function
Blob object introduction
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
互动送书-《Oracle DBA工作笔记》签名版
Qinglong panel - today's headlines
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
ROS bridge notes (05) - Carla_ ackermann_ Control function package (convert Ackermann messages into carlaegovehiclecontrol messages)
JS quick start (I)
Complete linear regression manually based on pytoch framework
追风赶月莫停留,平芜尽处是春山