Java泛型解析(02):通配符限定
Posted zhchoutai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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):通配符限定的主要内容,如果未能解决你的问题,请参考以下文章