Kotlin的型变解析(协变、逆变和不变)
一、首先来看一个例子
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:型变点违例

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
代码审计之DocCms漏洞分析
0x01 前言 DocCms[音译:稻壳Cms] ,定位于为企业、站长、开发者、网络公司、VI策划设计公司、SEO推广营销公司、网站初学者等用户 量身打造的一款全新企业建站、内容管理系统,服务于企业品牌信息化建设,也适应用个人、门户网站建设! 0x02 环境搭建 DocCms官网:http://www.doccms.com 程序源码:DocCms2016 下载地址:https://pan.baidu.com/s/1pLclifL 0x03 SQL注入 代码分析 在/content/search/index.php中,首先对参数keyword进行非法字符检测, 进一步追溯checkSqlStr函数,看代码如何过滤,在/inc/function.php中 checkSqlStr函数对传入的字符串进行正则匹配,检测是否函数非法字符。 继续看在/content/search/index.php中的get_search_result函数: 参数keyword进行非法字符检测后,进行url解码,然后拼接到SQL语句中执行。 如果我们传入双重url编码的字符串,将绕过非法字符检测,然后经urldec...
-
下一篇
C++函数中return语句的使用方法
** C++中的return语句是函数中一个重要的语句,return语句用于结束当前正在执行的函数,并将控制权返回给调用此函数的函数。 return语句有两种形式: return; return expression**; 1、没有返回值的函数 a.不带返回值的return语句只能用于返回类型为void的函数,return语句是为了引起函数的强制结束,这种用法类似于循环结构中的break语句的作用。eg: void swap(int &v1,int &v2) { if(v1==v2) return; int temp=v2; v2=v1; v1=tmp; } b.返回类型为void的函数通常不能使用第二种形式的return语句,但是,它可以返回另一个返回类型同样是void的函数的调用结果。eg: void do_swap(int &v1,int &v2) {int temp=v2; v2=v1; v1=tmp; } void swap(int &v1,int &v2) { if(v1==v2) return false; return d...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- MySQL数据库在高并发下的优化方案
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS8编译安装MySQL8.0.19
- SpringBoot2全家桶,快速入门学习开发网站教程