Java泛型解析(02):通配符限定
public class ArrayUtil {
public static <T> T max(T[] array) {
if (array == null || 0 == array.length) { return null ;}
T max = array[0];
for (int i = 1; i < array.length; i++) {
if (max.compareTo(array[i]) < 0) {max = array[i];}
}
return max;
}
}
public class ArrayUtil {
public static <T extends Comparable<T>> T max(T[] array) {
if (array == null || 0 == array.length) { return null ;}
T max = array[0];
for (int i = 1; i < array.length; i++) {
if (max.compareTo(array[i]) < 0) {max = array[i];}
}
return max;
}
}
<T extends Bounding Type>,表示T类型应该是绑定类型及其子类型(subType),T和绑定类型能够是类或者接口,使用extendskeyword由于它更接近于子类的概念,另外Java设计者并不打算为Java加入新的keyword如:sub
<T extends Runnable & Serializable>
<T extends Runnable & Serializable & ArrayList> // 错误
<T extends Runnable & ArrayList & Serializable> // 错误
<T extends ArrayList & LinkedList & Serializable> // 错误
<T extends ArrayList & Runnable& Serializable> // 正确
<T super File & Runnable> // 错误
public static <T super Runnable> void test(T runner) {
runner.run();
}
不管从设计角度,还是从后期扩展的角度。都是说只是去的。
Couple<? extends Employee>
public static void printWife(Couple<Employee> couple) {
Employee wife = couple.getWife();
System. out.println(wife);
}
所以要想让Manager也能结婚并打印其wife,须要更改我们的设计了:
public static void printWife(Couple<? extends Employee> couple) {
Employee wife = couple.getWife();
System. out .println(wife);
}
public static <T extends Comparable<T>> T max(T[] array) {...}
public static void printWife(Couple<? extends Employee> couple) {...}
而后者中不存在类型參数的定义,max方法參数的类型是预先定义好的Couple类型,使用者无法在使用的时候指定其它类型,但能够有限制地指定Couple中的泛型參数的类型,
extends Employee 自身不能单独使用,能够理解为仅仅能寄生在其它泛型类中,作为泛型类一个详细的类型參数,通经常使用于定义阶段,如以下:
public static ? extends Employee printWife() {...} // 错误
public static void printWife(? extends Empoyee employee) {...} // 错误
Couple<? super Manager>
Couple<Manager> couple = new Couple<Manager>(new Manager(),new Manager());
Couple<? extends Employee> couple2 = couple;
Employee wife = couple2.getWife();
// couple2.setWife(new Manager()); // 此处编译错误
?
extends Employee getWife() {...}
void setWife(? extends Employee) {...}
所以通配符的子类限定适用于读取。
? super Manager getWife() {...}
void setWife(? super Manager) {...}
"表示,而且也只能用于指定泛型类的类型參数中。如Couple<?>的形式,此时getWife和setWife方法如:
?
getWife() {...}
void setWife(?) {...}
public static boolean isCoupleComplete(Couple<?
> couple) {
return couple.getWife() != null && couple.getHusband() != null;
}
public static <T> boolean isCoupleComplete(Couple<T> couple) {
return couple.getWife() != null && couple.getHusband() != null ;
}
public static <T extends Employee> boolean isCoupleComplete(Couple<T> couple) {
return couple.getWife() != null && couple.getHusband() != null ;
}
这里考虑三种通配符的捕获
extends Employee> couple:getWife返回Employee
super Manager> couple:无法捕获,getWife返回Object
> couple:无法捕获,getWife返回Object
public static void print(Couple<?> couple) {
printHelp(couple);
}
public static <T> void printHelp(Couple<T> couple) {
T husband = couple.getHusband();
T wife = couple.getWife();
couple.setHusband(wife);
couple.setWife(husband);
System. out.println(husband);
System. out.println(wife);
}
如Couple<?
>、Couple<? extends Employee> 、Couple<? super Manager>
Java泛型解析(02):通配符限定的更多相关文章
- Java泛型解析(03):虚拟机运行泛型代码
Java泛型解析(03):虚拟机运行泛型代码 Java虚拟机是不存在泛型类型对象的,全部的对象都属于普通类,甚至在泛型实现的早起版本号中,可以将使用泛型的程序编译为在1.0虚拟机上可以执行的 ...
- Java泛型解析(04):约束和局限性
Java泛型解析(04):约束和局限性 前两节.认识和学习了泛型的限定以及通配符.刚開始学习的人可能须要一些时间去体会到泛型程序设计的优点和力量,特别是想成为库程序猿的同学就须要下 ...
- Java泛型解析(01):认识泛型
Java泛型解析(01):认识泛型 What Java从1.0版本号到如今的8.中间Java5中发生了一个非常重要的变化,那就是泛型机制的引入.Java5引入了泛型,主要还是为了满足在199 ...
- Java泛型中的通配符T,E,K,V
Java泛型中的通配符T,E,K,V 1.泛型的好处 2.泛型中的通配符 2.1 T,E,K,V,? 2.2 ?无界通配符 2.3 上界通配符 < ? extends E> 2.4 下界通 ...
- Java泛型中的通配符
Java泛型中的通配符可以直接定义泛型类型的参数.而不用把该函数定义成泛型函数. public class GenericsTest { public static void main(String[ ...
- 关于JAVA泛型中的通配符类型
之前对JAVA一知半解时就拿起weiss的数据结构开始看,大部分数据结构实现都是采取通配符的思想,好处不言而喻. 首先建立两个类employee和manager,继承关系如下.其次Pair类是一个简单 ...
- Java泛型二:通配符的使用
原文地址http://blog.csdn.net/lonelyroamer/article/details/7927212 通配符有三种: 1.无限定通配符 形式<?> 2.上边界限定 ...
- JAVA 泛型中的通配符 T,E,K,V,?
前言 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许开发者在编译时检测到非法的类型. 泛型的本质是参数化类型,也就是说所操作的数据 ...
- 【转】聊一聊-JAVA 泛型中的通配符 T,E,K,V,?
原文:https://juejin.im/post/5d5789d26fb9a06ad0056bd9 前言 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型 ...
随机推荐
- 10. Spring Boot JDBC 连接数据库
转自:https://blog.csdn.net/catoop/article/details/50507516
- python关于字典的操作
https://www.cnblogs.com/RENQIWEI1995/p/7931374.html 最常用的代码举例: dict = {'Name': 'Zara', 'Age': 7, 'Cla ...
- PatentTips - Method for guest operating system integrity validation
BACKGROUND The embodiments relate to guest operating system integrity validation, and more particula ...
- java set转list,数组与list的转换
读zookeeper的源码(zookeeper.java)时发现的,平时没有怎么注意: private final ZKWatchManager watchManager; List<Strin ...
- 关于VUE的安装和一些简单属性
安装vue 安装前初始化package.json 主要用来描述自己的项目,记录安装过得文件有哪些,在当前文件夹下生产json 安装vue --save(-S)代表项目依赖 --save-dev(-D) ...
- “-bash: !”: event not found"、echo > sudo permission denied
1. "-bash: !": event not found" 比如当我们在 linux 命令行输入echo "Reboot your instance!&qu ...
- ElasticSearch 2.4
ES是一个基于Lucene的分布式全文搜索服务器,和SQL Server的全文索引(Fulltext Index)有点类似,都是基于分词和分段的全文搜索引擎,具有分词,同义词,词干查询的功能,但是ES ...
- 每日技术总结:vue router传参方式,js获取设备高度
今天貌似没什么问题,23333…… 1.vue router 路由传参的方式 应用情景:从分类页(category.vue)进入商品列表页(list.vue),需要传递商品分类id(catId),商品 ...
- 高级Java工程师必备 ----- 深入分析 Java IO (三)
概述 Java IO即Java 输入输出系统.不管我们编写何种应用,都难免和各种输入输出相关的媒介打交道,其实和媒介进行IO的过程是十分复杂的,这要考虑的因素特别多,比如我们要考虑和哪种媒介进行IO( ...
- [TypeScript] Using Interfaces to Describe Types in TypeScript
It’s easy to pass the wrong value to a function. Typescript interfaces are great because they catch ...