0
我有類三角形與圓從形狀繼承,以及能夠吸引圓形或三角形,像這樣一類應用:多態形狀類繪製最近創建的任何形狀?
public class Application extends JPanel{
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Shape[] shapes = new Shape[4];
Circle circle1 = new Circle(100,100,80);
Triangle tri1 = new Triangle(20,40,40);
Circle circle2 = new Circle(300,300,100);
Triangle tri2 = new Triangle(30,30,30);
shapes[0] = circle1;
shapes[1] = tri1;
shapes[2] = circle2;
shapes[3] = tri2;
for(Shape shape : shapes){
shape = (Shape) shape;
shape.draw(g, this);
}
}
}
代碼繪製一個無誤差圈內,但得出的三角形始終是第二個(tri2
)。我四處遊玩,意識到繪製的三角形總是最後定義的三角形。我想知道爲什麼會出現這種情況?
如果你沒有向我們展示你的Shape類或你的Triangle類,那麼怎麼可能有人告訴你你做錯了什麼? (雖然我的猜測是你有一些標記爲「靜態」的字段不應該是)。 –
你的意思是「繪製的三角形將總是繪製最後定義的東西」?例如。你能提供一些例子輸出你現在看到的和你期望的結果嗎? – n247s
對不起,你們仍然習慣於在這裏問問題的細節缺乏。 – Jeni