当前位置: 首页 > news >正文

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

文章目录

  • 1. 基本概念
  • 2. 理解泛型擦除
  • 3. 不变,协变,逆变
    • 3.1 不变
    • 3.2 协变
      • 1. 基础定义
      • 2. 案例分析:接口类List
    • 3.3 逆变
  • 4. 星投影
  • 5. 内联实化

1. 基本概念

2. 理解泛型擦除

3. 不变,协变,逆变

3.1 不变

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 Operationsoverride val size: Intoverride fun isEmpty(): Booleanoverride fun contains(element: @UnsafeVariance E): Booleanoverride fun iterator(): Iterator<E>// Bulk Operationsoverride 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 逆变

4. 星投影

5. 内联实化

http://www.dtcms.com/a/403248.html

相关文章:

  • h.265格式的视频在浏览器无法正常播放,使用ffprobe转为h.264
  • sysbench mysql 单表 insert 压测 , 自定义lua测试脚本
  • 石家庄制作网站的公司电商流量平台
  • 二手车网站html模板西宁网站设计
  • 零成本上线动态博客:用 Rin + Cloudflare 部署个人博客的完整指南
  • 家用净水器DIY,75G经典5级Ro净水器
  • 七、OpenCV中的视频的读写
  • ClipboardApp —— Mac 专属轻量级剪切板助手(开源)
  • 【开题答辩全过程】以 LoveEditing视频编辑社团网站为例,包含答辩的问题和答案
  • ARM芯片架构之CoreSight ROM Table 的SoC设计思路
  • 门户网站建设的平台搭建hello md5 wordpress
  • LeetCode 485.最大连续1的个数
  • 【综述】Processes at the intracellular scale 细胞内尺度的过程
  • 截取字符串
  • 【LeetCode热题100(29/100)】删除链表的倒数第 N 个结点
  • PyTorch 数据处理与可视化全攻略
  • 【LeetCode】912. 排序数组、手撕快速排序
  • 国内企业建站模板淘宝代运营去哪里找
  • VTK基础(05):VTK的渲染窗口嵌入到QT的控件当中
  • 深入解析 List 容器组件:构建高效、可交互的列表解决方案
  • 06.容器存储
  • 自己做的网站为何手机不能浏览快闪ppt模板免费下载
  • 动态内存管理 干货2
  • pdf转图片:pdf2image
  • 高校档案网站建设网站如何做成app
  • 画质及画面刷新率如何调整?正式升级!2K240帧原画级教程
  • 兰州网站的建设群晖搭建的wordpress外网访问
  • Redis常见八股文
  • Rust HashMap详解
  • 定制网站开发哪家强千旺crm客户管理系统