当前位置:网站首页>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
}
边栏推荐
- Bayes' law
- Interview questions (CAS)
- Basic use of CTF web shrink template injection nmap
- Leetcode medium question my schedule I
- Es FAQ summary
- Easy to understand SSO
- 雅思考试自己的复习进度以及方法使用【日更版】
- It took "7" years to build the robot framework into a micro service
- Network learning (I) -- basic model learning
- [quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
猜你喜欢
buureservewp(2)
Force buckle 144 Preorder traversal of binary tree
CTF-WEB shrine模板注入nmap的基本使用
漏洞复现-easy_tornado
漏洞复现-Fastjson 反序列化
使用 Nocalhost 开发 Rainbond 上的微服务应用
The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
Avatary's livedriver trial experience
LeetCode简单题之判断一个数的数字计数是否等于数位的值
拓维信息使用 Rainbond 的云原生落地实践
随机推荐
ROS Bridge 笔记(05)— carla_ackermann_control 功能包(将Ackermann messages 转化为 CarlaEgoVehicleControl 消息)
复杂网络建模(二)
CTF-WEB shrine模板注入nmap的基本使用
面试题(CAS)
Leetcode simple question: find the K beauty value of a number
机器人教育在动手实践中的真理
【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
Linux Installation MySQL 8.0 configuration
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
Avatary的LiveDriver试用体验
在Rainbond中一键部署高可用 EMQX 集群
jeeSite 表单页面的Excel 导入功能
Recursive construction of maximum binary tree
太真实了,原来自己一直没有富裕起来是有原因的
饥荒云服管理脚本
DNS server configuration
青龙面板-今日头条
Complex network modeling (II)
LeetCode简单题之字符串中最大的 3 位相同数字