Java泛型中有三种通配符
1、<? extends xxx>子类界定
2、<? super xxx>超类界定
3、<?>无限定
1、<? extends xxx>子类界定
package com.zjh.test;
import java.io.Serializable;
import com.zjh.test.model.Animal;
import com.zjh.test.model.Horse;
import com.zjh.test.model.LittleHorse;
public class Pair {
public static <T extends Comparable & Serializable> Animal<T> getAnimal(
T[] t) {
T min = t[0];
T max = t[0];
for (int i = 1; i < t.length; i++) {
T key = t[i];
if (min.compareTo(key) > 0) {
min = key;
}
if (max.compareTo(key) < 0) {
max = key;
}
}
Animal animal = new Animal<T>(min, max);
return animal;
}
public static <T extends Comparable & Serializable> String getName(T t) {
return t.toString();
}
public static String getHorseName(Animal<? extends Horse> a) {
return a.getBigger().toString();
}
public static void main(String args[]) {
// Animal animal = getAnimal(new String[] { “zjh”, “zcx”, “llj”, “zst”,
// “lly” });
// System.out.println(animal.toString());
//
// getName(animal);
LittleHorse lh1 = new LittleHorse(“zjh”, 26);
LittleHorse lh2 = new LittleHorse(“llj”, 27);
Animal<LittleHorse> animal = new Animal<LittleHorse>(lh1, lh2);
System.out.print(getHorseName(animal));
}
}
package com.zjh.test.model;
import java.io.Serializable;
public class Animal<T extends Comparable & Serializable> {
@Override
public String toString() {
// TODO Auto-generated method stub
String s = “minName:” + t1.toString() + “,maxName:” + t2.toString();
return s;
}
public T getBigger(){
if(t1.compareTo(t2) > 0 ){
return t1;
}else{
return t2;
}
}
private T t1;
private T t2;
public Animal() {
t1 = null;
t2 = null;
}
public Animal(T t1, T t2) {
this.t1 = t1;
this.t2 = t2;
}
}
2、超类限定
与子类限定,直观解释来说,子类限定的通配符用来从入参中读取信息,而父类限定的通配符用来写入代码执行结果信息。
从限定上来说,子类限定时,入参必须是限定类的子类泛型,而父类则是限定类的父类。
public static Animal<? super LittleHorse> getHorse(LittleHorse[] t,
Animal<? super LittleHorse> a) {
LittleHorse min = t[0];
LittleHorse max = t[0];
for (int i = 1; i < t.length; i++) {
LittleHorse key = t[i];
if (min.compareTo(key) > 0) {
min = key;
}
if (max.compareTo(key) < 0) {
max = key;
}
}
a.setT1(min);
a.setT2(max);
return a;
}
public static void main(String args[]) {
// Animal animal = getAnimal(new String[] { “zjh”, “zcx”, “llj”, “zst”,
// “lly” });
// System.out.println(animal.toString());
//
// getName(animal);
// LittleHorse lh1 = new LittleHorse(“zjh”, 26);
// LittleHorse lh2 = new LittleHorse(“llj”, 27);
// Animal<LittleHorse> animal = new Animal<LittleHorse>(lh1, lh2);
// System.out.print(getHorseName(animal));
LittleHorse lh1 = new LittleHorse(“zjh”, 26);
LittleHorse lh2 = new LittleHorse(“llj”, 27);
LittleHorse lh3 = new LittleHorse(“zcx”, 25);
LittleHorse lh4 = new LittleHorse(“zst”,10);
LittleHorse [] s = new LittleHorse[]{lh1,lh2,lh3,lh4};
Horse t1 = new Horse();
Horse t2 = new Horse();
Animal<Horse> animal = new Animal<Horse>(t1, t2);
//可以编译
Animal<?> animal2 = getHorse(s,animal);
System.out.println(animal2.getBigger().toString());
LittleHorse lt1 = new LittleHorse();
LittleHorse lt2 = new LittleHorse();
Animal<LittleHorse> animal_test = new Animal<LittleHorse>(lt1, lt2);
//无法编译
//getHorse(animal_test);
}
3、<?>无限定