1、extends 上边界不能放,因为上边界作用下,不知道要放什么样的类型 所以会报错,但是可以取
2、super 下边界不能取,因为在下边界的作用下,不知道要取出来什么样的数据(本身类型,object) 但是可以放
public class Test {
//上界通配符
class Fruit {}
class Apple extends Fruit {}
class Plate<T extends Fruit>{
private T item;
public Plate(T t){item=t;}
public void set(T t){item=t;}
public T get(){return item;}
}
public void testOne (){
// Plate<Fruit> p=new Plate<Apple>(new Apple());
Plate<? extends Fruit> p=new Plate<Apple>(new Apple());
//上界通配符不能放
// p.set(new Apple());
Fruit fruit = p.get();
}
//下界通配符
class PlateNew<T>{
private T item;
public PlateNew(T t){item=t;}
public void set(T t){item=t;}
public T get(){return item;}
}
public void testTwo (){
PlateNew<? super Fruit> p=new PlateNew<Fruit>(new Fruit());
//存入元素正常
p.set(new Fruit());
p.set(new Apple());
//读取出来的东西只能存放在Object类里。
// Apple newFruit3=p.get(); //Error
// Fruit newFruit1=p.get(); //Error
Object newFruit2=p.get();
}
}