【Kotlin进阶】泛型的高级特性

1. 基本概念

1.1 泛型的作用

  • 编译时可以检查出错误
  • 运行时可以自动进行类型转换

1.2 泛型的使用

  • fun find(t: T): T?{}
  • fun ArrayList.find (t: T): T? {}

1.3 where关键字

当一个类型参数指定多个约束时,在 Java 中使用&连接多个类、接口。

classClassA { }
interface InterfaceB { }
public class MyClass<T extends ClassA & InterfaceB> {
    Class<T> variable;
}

Kotlin使用where关键字


open class ClassA

interface InterfaceB

class MyClass<T>(var variable: Class<T>) where T : ClassA, T : InterfaceB

2. 理解泛型擦除

2.1 基本理解

  • 泛型信息仅在编译时用于类型检查
  • 泛型参数在运行时不保留
  • 泛型参数被替换为上界(如 Any?)

2.2 为什么引入泛型擦除

解决Java中向后兼容的问题,即老版本的代码可以在新版本的jvm上运行。比如ArrayList list = new ArrayList()

2.3 泛型擦除的矛盾

  • 比如在序列化或者反序列化场景下需要知道运行时泛型参数的类型

2.4 内联函数获取泛型

在这里插入图片描述

3. 不变,协变,逆变

3.1 不变

  • 在Java中List< String > 不能赋值给List< Object >
  • Kotlin允许,多了一个关键字out

在这里插入图片描述

  • Java和Kotlin中List的定义

在这里插入图片描述

3.2 协变

1. 基础定义

  • 如果 A 是 B 的子类型,并且Generic< A> 也是 Generic< B > 的子类型,那么 Generic 可以称之为一个协变类。
  • 需要在泛型参数前面加上 out 关键字
  • 无法添加元素,只能读取内容(没有add方法)
  • UnsafeVariance 在协变(Covariance)中的应用主要是用于打破 Kotlin编译器对泛型类型参数使用位置的限制,从而允许将协变类型参数(out T)用在一些原本不允许的位置(如函数参数、可变属性等),前提是开发者自己能保证类型安全。

2. 案例分析:接口类List

前提是List类使用了out关键字定义泛型参数类型;
String是Any的子类,因此List< String >是List< Any >的子类

        val stringList : List<String> = ArrayList<String>()
        val anyList : List<Any> = stringList
        //假设有add方法
        anyList.add(1) 
        val str: String = anyList.get(0) //出错
        
  • List源码
  • 使用了out关键字声明
  • 没有add方法
  • UnsafeVariance的使用,如何理解大白话讲,比如contains中E被当作输入参数(所谓的逆变点),因此就需要加上该注解
public interface List<out E> : Collection<E> {
    // Query Operations

    override val size: Int
    override fun isEmpty(): Boolean
    override fun contains(element: @UnsafeVariance E): Boolean
    override fun iterator(): Iterator<E>

    // Bulk Operations
    override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean

    // Positional Access Operations
    /**
     * Returns the element at the specified index in the list.
     */
    public operator fun get(index: Int): E

    // Search Operations
    /**
     * Returns the index of the first occurrence of the specified element in the list, or -1 if the specified
     * element is not contained in the list.
     */
    public fun indexOf(element: @UnsafeVariance E): Int

    /**
     * Returns the index of the last occurrence of the specified element in the list, or -1 if the specified
     * element is not contained in the list.
     */
    public fun lastIndexOf(element: @UnsafeVariance E): Int

    // List Iterators
    /**
     * Returns a list iterator over the elements in this list (in proper sequence).
     */
    public fun listIterator(): ListIterator<E>

    /**
     * Returns a list iterator over the elements in this list (in proper sequence), starting at the specified [index].
     */
    public fun listIterator(index: Int): ListIterator<E>

    // View
    /**
     * Returns a view of the portion of this list between the specified [fromIndex] (inclusive) and [toIndex] (exclusive).
     * The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.
     *
     * Structural changes in the base list make the behavior of the view undefined.
     */
    public fun subList(fromIndex: Int, toIndex: Int): List<E>
}

3.3 逆变

1. 基础定义

  • 如果 A 是 B 的子类型,并且Generic< B> 反过来是 Generic< A > 的子类型,那么 Generic可以称之为一个逆变类。

  • in关键字不能作为方法的返回值类型,但可以作为方法的参数类型

  • 等价于Java中的<? super T>

  • 没有get方法

2. 案例分析

在这里插入图片描述
在这里插入图片描述

4. 星投影

星号投影用来表明“不知道关于泛型实参的任何信息”。

类似于 Java 中的无界类型通配符?, Kotlin 使用星号投影*。

*代指了所有类型,相当于Any?。

例如:MutableList<*> 表示的是 MutableList<out Any?>

fun main() {
    val list1 = mutableListOf<String>()
    list1.add("string1")
    list1.add("string2")
    printList(list1)

    val list2 = mutableListOf<Int>()
    list2.add(123)
    list2.add(456)
    printList(list2)
}

fun printList(list: MutableList<*>) {

    println(list[0])
}

正是由于使用 out 修饰以及星号投影的类型不确定性,会导致写入的任何值都有可能跟原有的类型冲突。因此,星号投影不能写入,只能读取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值