在泛型中查看此示例:
// abstract class
public abstract class Shape {
public abstract void draw(Canvas c);
}
//implementation of shape class
public class Rectangle extends Shape {
private int x, y, width, height;
public void draw(Canvas c) { ... }
}
//function to add rectangle object in a list
public void addRectangle(List<? extends Shape> shapes) {
shapes.add(0, new Rectangle()); // compile-time error!
}
为什么这是编译错误?我无法弄清楚它是类型列表中的形状,它们是形状的子类,而矩形是形状的子类。
请帮我弄清楚这个查询。