你好,我有一个类在Java中绘制了一颗明星,它就像一个魅力一样。在此之后,我扩展了Star类以创建具有扩展可能性的另一个明星(在这种情况下颜色必须不同)在java中重写构造函数
由于某种原因,在我的面板中调用类并仅为构造函数提供参数时孩子班的颜色似乎工作。
这里是我的代码
public class Star {
protected int radius;
protected int xmiddelpunt;
protected int ymiddelpunt;
protected static Color color;
public Star(int radius, int x, int y, Color color) {
xmiddelpunt = x;
ymiddelpunt = y;
this.radius = radius;
this.color = color;
}
}
和扩展类
public class StarRed extends Star {
protected int x, y;
protected static Color color;
Random red = new Random();
public StarRed(int radius, int x, int y, Color color) {
super(radius, x, y, color);
this.radius = radius;
this.x = x;
this.y = y;
this.color = color;
}
}
我的面板类的构造函数如下:
ArrayList<Star> stars = new ArrayList<Star>();
ArrayList<StarRed> rs = new ArrayList<StarRed>();
public HeavenPanel() {
setBackground(Color.blue); // geef het paneel een blauwe kleur
this.addMouseWheelListener(this); // set de mouselistener
for(int i = 0; i < 10; i++) {
stars.add(new Star (r.nextInt(30 + 50), r.nextInt(10 + 701), r.nextInt(10 + 701), Color.yellow));
}
for(int k = 0; k < 10; k++) {
rs.add(new StarRed(40, r.nextInt(30 + 50), r.nextInt(30 + 50), Color.red));
}
}
如果您正在扩大班级,它将从父母已完成的工作中获利。所以,不要在'StarRed'中复制你已经在'Star'中管理的所有属性(所有这些属性)。 – SJuan76
你应该发布你对这些列表所做的事情,否则我们不知道GUI中发生了什么。 – SJuan76