2012-12-05 124 views
2

你好,我有一个类在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)); 
    } 

} 
+1

如果您正在扩大班级,它将从父母已完成的工作中获利。所以,不要在'StarRed'中复制你已经在'Star'中管理的所有属性(所有这些属性)。 – SJuan76

+0

你应该发布你对这些列表所做的事情,否则我们不知道GUI中发生了什么。 – SJuan76

回答

6

第一个问题:

protected static Color color; 

这意味着,场(你有两个的...)是在整个类型共享。我本以为这是一个实例字段,所以不同的星星可以是不同的颜色。相反,所有明星都是相同的颜色,除非你有在StarRed一些代码,使用color领域,在这种情况下,你可能有明星颜色...但它仍然是不正确的。

问题二:,你StarRed类声明自己的字段xy,并且color尽管他们也正在超类中声明。尽管已经在超类构造函数中设置了,但您仍然可以设置超类radius字段的值。

基本上这一切都有点感到迷惘。你应该计算出与类型相关的信息,而不是任何特定的实例(在这种情况下,应该是一个静态字段)以及哪些信息与单个实例相关联(在这种情况下,这些信息应该是实例字段)。你应该几乎永远不会在一个子类和一个超类中使用相同的字段名称 - 我个人建议使所有字段私有(除了可能的常量)。

最后,为什么会在StarRed构造要采取Color呢?它不应该总是红色吗?

+0

啊谢谢!你我有点困惑,但整个问题都是静态的!我删除了这个以及我的子类的构造函数中的x和y,非常感谢! – Reshad

+0

有人认为我不明白...为什么我不能给一个this.color = color.red;在我的子类构造器中没有使它在超类中是静态的? – Reshad

+0

@Reshad:你应该可以,虽然它是一个受保护的领域。 (我建议*不要*在子类中设置它,因为您已经将它设置在超类中。)基本上,只存储任何一条信息,并且只将它放在一个地方。 –

5

要覆盖静态变量颜色。

Static关键字指类的所有实例具有相同的颜色。

所以,家长和孩子都指的是同一个静态变量。

因此,因为您以后设置子颜色只有子颜色的作品。

更改您的密码和删除静态

+0

你不能*覆盖*一个字段。父母和孩子实际上是指*不同的*静态变量,因为每个类声明自己的静态变量。 –

1

至于其他的答案说,首先从public static Color color去除静电。 此外,您不需要在StarRed类中重新声明Star的字段。从您的Random red = new Random()声明中,我假设您要在StarRed中进行一些计算以确定红色的色调,因此您需要添加另一个受保护的Star构造函数,该构造函数省略了设置颜色的操作。你使用StarRedStar的公共构造函数也会使用它,但是另外设置星号的颜色。

您的代码应该是这样的:

public class Star { 

    protected int radius; 
    protected int xmiddelpunt; 
    protected int ymiddelpunt; 
    protected Color color; 

    public Star(int radius, int x, int y, Color color) {   
     this(x,y,radius) 
     this.color = color; 
    } 

    protected Star(int radius, int x, int y) { 
     xmiddelpunt = x; 
     ymiddelpunt = y; 
     this.radius = radius; 

     this.color = color; 
    } 


} 

和扩展类

public class StarRed extends Star { 

    Random red = new Random(); 

    // Overrides Star constructor 
    public StarRed(int radius, int x, int y, Color color) { 
     super(radius, x, y); // Call protected superconstructor (radius, x,y) 
     // Now we set the color, so the star is red (example code!) 
     this.color = Color.RED; 
     // You can still use color e.g. to define blue and green components of this.color 

    } 
} 

顺便说一句:如果你如从StarRed构造函数中删除颜色变量,那么您只需要过载构造函数Star

我希望它有帮助:)

+0

如果我可以添加一些东西,请删除this.color = color;从你的第二个构造函数的星 – user902383

+0

啊帮助!谢谢! – Reshad