以下代码是抽象类的一部分,该抽象类旨在被子类化以管理特定类型的 Shape。(它实际上是特定类的存储库,但现在不相关)
protected ArrayList<? extends Shape> shapesOfSpecificType;
public addShape(Shape shape){
getShapes; //make sure shapeOfSpecificType is instantiated and load stuff from db
shapesOfSpecificType.add(shape); //gives an compile error
}
如何接受 Shape 子类作为 addShape 中适合添加到 ArrayList 的参数?