Kotlin的型变解析(协变、逆变和不变)

简介: 一、首先来看一个例子import java.util.*/** * @author:wangdong * @description:型变 */fun main(args: Array) {}/*...

一、首先来看一个例子

import java.util.*

/**
 * @author:wangdong
 * @description:型变
 */

fun main(args: Array<String>) {

}

/**
 * 定义一个类,实现了List接口
 * 协变out(返回值只读类型),逆变in(通常是写入的),可读可写就是不变了
 */
class MyLisøt<in E>{
     val size: Int
        get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.

     fun contains(element: @UnsafeVariance E): Boolean {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

     fun containsAll(elements: Collection<E>): Boolean {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}

二、看一下协变、逆变和不变的例子

import java.util.*

/**
 * @author:wangdong
 * @description:协变、逆变、不变
 */

fun main(args: Array<String>) {

    //定义个协变
    //List 不是java.util中的list
    //定义一个numberlist的引用。
    //List<number>是只读的接口
    //number是int的父类,所以List<Number>是List<Int>的父类
    val numberList: List<Number> = listOf(1,3,5)
    //public interface List<out E> : Collection<E> {

    //定义一个逆变的例子
    //恰恰相反public interface Comparable<in T> {,是可写的接口
    //所以Comparable<Int> 是 Comparable<Any>的父类
    val intComparable: Comparable<Int> = object: Comparable<Any>{
        /**
         * Compares this object with the specified object for order. Returns zero if this object is equal
         * to the specified [other] object, a negative number if it's less than [other], or a positive number
         * if it's greater than [other].
         */
        override fun compareTo(other: Any): Int {
            return 0
        }
    }

    //定义一个不变的例子
    //报错部分,都说明MutableList<T>不是mutableListOf<非T>的父类
    //只有在T与T相同的情况下,才是ok的
    //val numberArrayList: MutableList<Number> = mutableListOf<Int>(1,5,7)
    //val numberArrayList: MutableList<Int> = mutableListOf<Number>(1,5,7)
    val numberArrayList: MutableList<Int> = mutableListOf<Int>(1,5,7)
    //MutableList接口public interface MutableList<E> : List<E>, MutableCollection<E> {
    //既没有out,也没有in,所以这个接口是可读写的,所以它是不变的
}

三、协变、逆变和@UnsafeVariance
1.协变点:返回值类型是泛型参数类型
2.逆变点:入参类型是泛型参数类型
3.@UnsafeVariance:型变点违例

目录
相关文章
|
11月前
|
XML 存储 Java
Kotlin 进阶 | 不变型、协变、逆变
Kotlin 进阶 | 不变型、协变、逆变
10888 1
|
安全 Java 编译器
重学Kotlin之泛型的逆变和协变
泛型的逆变和协变
233 0
|
XML JavaScript Java
Kotlin/Java解析XMl文件的四种方式
Kotlin/Java解析XMl文件的四种方式
|
Java 编译器 C++
Kotlin 范型之协变、逆变
Kotlin 范型之协变、逆变
309 0
Kotlin 范型之协变、逆变
DHL
|
XML 算法 前端开发
为数不多的人知道的 Kotlin 技巧及解析(三)
Google 引入 Kotlin 的目的就是为了让 Android 开发更加方便,自从官宣 Kotlin 成为了 Android 开发的首选语言之后,已经有越来越多的团队,在项目使用 Kotlin。
DHL
258 0
为数不多的人知道的 Kotlin 技巧及解析(三)
|
Kotlin
【Kotlin】循环控制流 ( for 循环 | Iterator 遍历形式 | Iterator 遍历要求 | IntArray 源码解析 )
【Kotlin】循环控制流 ( for 循环 | Iterator 遍历形式 | Iterator 遍历要求 | IntArray 源码解析 )
299 0
|
Kotlin
kotlin 泛型-协变、逆变
在java中,假设有一个泛型接口 GenericClass , 该接口中不存在任何以 T 作为参数的方法,只是方法返回 T 类型值: 那么,在 GenericClass 为此,我们必须声明对象的类型为 GenericClass
555 0
|
安全 Java Android开发
Kotlin 设计模式解析之单例
### 单例模式介绍 单例模式是一个比较简单的设计模式,同时也是挺有意思的一个模式,虽然看起来简单,但是可以玩出各种花样。比如 Java 当中的懒饿汉式单例等。 #### 什么是单例 单例模式的定义: > Ensure a class only has one instance, and provide a global point of access to it. 简单来说
1718 0
|
XML JavaScript Java
Kotlin/Java解析XMl文件的四种方式
des... 四种解析方式: DOM SAX JDOM DOM4J 解析目标-books.xml文件 XML深入浅出 Imooc 2014 89 Java从入门到精通 Imooc 369 1.
1359 0
|
Java Scala Kotlin
JVM语言生态结构原理图 从Java,Kotlin,Scala,Groovy等语言的编译、执行全过程图示解析
JVM语言生态结构原理图 从Java,Kotlin,Scala,Groovy等语言的编译、执行全过程图示解析 JVM语言生态 by 陈光剑.png
1044 0

推荐镜像

更多