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> // 错误
我们实现自己的泛型是提供后期应用程序猿使用的。限定一个子类,这就须要我们通过子类来调用方法,而调用的方法体则是这个类的超类的实例。继承结构越往上就可能是abstract的。或者是interface。抽象类和接口是无法实例化对象。这样的反设计让调用面临失败。一旦限定的这个类就是抽象的或者是接口,必然会造成这个泛型类或泛型方法无法使用,导致设计失败。举个样例:
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>
=====【感谢亲阅读寻常心的文章,亲若认为此文有帮助,顶一顶呗。亲的支持将给我前进的动力】=====