如何填写列表<?延伸形状>与派生的圆形和矩形对象? [英] How to fill List<? extends Shape> with derived Circle and Rectangle objects?
问题描述
public abstract class Shape {
public abstract void draw(Canvas c) ;
}
public class Circle扩展形状{
private int x,y,radius;
public void draw(Canvas c){...}
}
public class Rectangle extends Shape {
private int x,y,width,height;
public void draw(Canvas c){...}
}
这些类可以绘制在画布上:
public class Canvas {
public void draw(Shape s){
s.draw(this);
}
}
我想将所有的Circle和Rectangle对象放入一个List,并在单个迭代语句中将它们全部绘制,如下所示:
public void drawAll(List< ;? extends Shape> ;形状){
(形状s:形状){
s.draw(this);
}
}
一切都在编译。
如果我创建一个测试类并尝试创建如下列表,则问题开始:
public class Main {
type
public static void main(String [] args){
Canvas canvas = new Canvas();
列表< ;?延伸形状> list = new ArrayList<>();
Circle circle = new Circle();
Rectangle rectangle = new Rectangle();
list.add(circle); //类型List< capture#1-of?中的方法add(捕获#1 of?extends Shape)延伸形状>不适用于参数(Circle)
canvas.drawAll(list);
$ b正如代码中所记录的那样,我不能将Circle和Rectangle对象添加到List中。
问题:我应该如何修改代码,以便构建一个
列表与LT ;?延伸形状>
然后遍历该列表绘制形状。
在此先感谢。
发布编辑:谢谢!这是一个很大的突破。应该给SpaceTrucker和他的关键PECS链接的信贷,但这是不可能的。由于某种原因,我告诉自己我应该使用extends,并且从此不会对此作出任何质疑。尝试指定<$ c $ c> List
List< Shape> list = new ArrayList< Shape>();
Shape circle = new Circle();
Rectangle rectangle = new Rectangle();
list.add(circle);
list.add(矩形);
canvas.drawAll(list);
I have these simple classes:
public abstract class Shape { public abstract void draw(Canvas c); } public class Circle extends Shape { private int x, y, radius; public void draw(Canvas c) { ... } } public class Rectangle extends Shape { private int x, y, width, height; public void draw(Canvas c) { ... } }
These classes can be drawn on a canvas:
public class Canvas { public void draw(Shape s) { s.draw(this); } }
I would like to put all Circle and Rectangle objects in a single List and draw all of them in a single iteration statement, like this:
public void drawAll(List<? extends Shape> shapes) { for (Shape s : shapes) { s.draw(this); } }
Everything compiles.
The problem begins if I make a test class and try to create a list like this:
public class Main { public static void main(String[] args) { Canvas canvas = new Canvas(); List<? extends Shape> list = new ArrayList<>(); Circle circle = new Circle(); Rectangle rectangle = new Rectangle(); list.add(circle); // The method add(capture#1-of ? extends Shape) in the type List<capture#1-of ? extends Shape> is not applicable for the arguments (Circle) canvas.drawAll(list); } }
As is documented in the code, I cannot add Circle and Rectangle objects to the List.
Question: how should I modify the code such, that I can construct a
List<? extends Shape>
and next iterate over that List to draw the shapes.
thanks in advance.
Post Edit: Thanks! That was quite a breakthrough. Should have given the credits to SpaceTrucker and his crucial PECS link, but that wasn't possible. For some reason I had told myself that I should use extends and never questioned that assumption since.
解决方案Try to specify
List
typeList<Shape> list = new ArrayList<Shape>(); Shape circle = new Circle(); Rectangle rectangle = new Rectangle(); list.add(circle); list.add(rectangle); canvas.drawAll(list);
这篇关于如何填写列表<?延伸形状>与派生的圆形和矩形对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!