这个问题已经在这里有了答案:????????????>????????????What is PECS (Producer Extends Consumer Super)?????????????????????????????????????11个
我有一个问题,例如:
水果类
public class Fruit extends Food {
public static void main(String[] args) {
Plate super Fruit> plate = new Plate<>(new Food());
plate.setItem(new Apple());
plate.setItem(new Food());
}
static class Apple extends Fruit {
}
}
食品类
public class Food {
}
板类
public class Plate {
private T item;
public Plate(T t) {
item = t;
}
public T getItem() {
return item;
}
public void setItem(T item) {
this.item = item;
}
}
我不明白为什么
Plate super Fruit> plate = new Plate<>(new Food())
没有错误
但
plate.setItem(new Food())
是错误的
这两种方法有什么区别?
-全部,谢谢!