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 中引入的一个新特性, 泛型提供了编译时类型 ...
随机推荐
- iterm恢复默认设置
命令行执行以下命令即可: defaults delete com.googlecode.iterm2
- 有关Canvas的一点小事—图像绘制
1. 使用canvas绘制图像 什么是图像?在js中它就是一个<img src=””>,<img>有两种接收图像信息的方法,一个是直接链接到图像地址,一个使用base64数据 ...
- 00091_字符输入流Reader
1.字符输入流Reader (1)字符输入流Reader我们读取拥有中文的文件时,使用的字节流在读取,那么我们读取到的都是一个一个字节: (2)只要把这些字节去查阅对应的编码表,就能够得到与之对应的字 ...
- 洛谷——P1601 A+B Problem(高精)
https://www.luogu.org/problem/show?pid=1601#sub 题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑 ...
- HDOJ 5409 CRB and Graph 无向图缩块
无向图缩块后,以n所在的块为根节点,dp找每块中的最大值. 对于每一个桥的答案为两块中的较小的最大值和较小的最大值加1 CRB and Graph Time Limit: 8000/4000 MS ( ...
- 12.SpringBoot+MyBatis(XML)+Druid
转自:https://www.cnblogs.com/MaxElephant/p/8108342.html 主要是在Spring Boot中集成MyBatis,可以选用基于注解的方式,也可以选择xml ...
- vue使用(三)
本节目标:获取后端api数据 需求:一个按钮,点击之后将服务器上的数据获取到,并显示出来 方法一: 1. 准备工作, (1)安装官方插件 vuedemo02>npm install vue-re ...
- Testfan软件测试社区
1. http://ask.testfan.cn/article/902 Appium 服务端安装-windows2. http://ask.testfan.cn/article/1078 最新 ...
- 卡塔兰数(Catalan)
卡塔兰数(Catalan) 原理: 令h(0)=1,h(1)=1. 卡塔兰数满足递推式:h(n)=h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0)(n>=2) ...
- jQuery和CSS3炫酷GOOGLE样式的用户登录界面
这是一款使用jQuery和CSS3打造的GOOGLE样式的用户登录界面特效.该登录界面特效中,右上角的小问号和错误提示小图标使用SVG来制作.username和password输入框採用浮动标签特效. ...