ジェネリックでこの例を見る:
// 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!
}
なぜこれがコンパイルエラーになるのですか? type のリストにある shape は shape のサブクラスであり、rectangle は Shape のサブクラスであるため、わかりませんでした。
このクエリを理解するのを手伝ってください。