2

こんにちは、魅力のように機能する Java で星を描くクラスがあります。この後、Star クラスを拡張して、可能性を拡張した別の星を作成しました (この場合、色は異なる必要があります)。

クラスを呼び出してコンストラクターでパラメーターを指定すると、何らかの理由でパネルで子クラスの色のみが機能するようです。

ここに私のコードがあります

    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));
    }

}
4

3 に答える 3

6

最初の問題:

protected static Color color;

つまり、フィールド(2つあります...)はタイプ全体で共有されます。これはインスタンスフィールドであると予想していたので、星が異なれば色も異なる可能性があります。StarRed代わりに、フィールドを使用するコードがない限り、すべての星は同じ色です。colorその場合、2色の星がある可能性があります...しかし、それでも正しくありません。

2番目の問題:スーパークラスでも宣言されているにもかかわらず、クラスは、、、およびのStarRed独自のフィールドを宣言します。次に、スーパークラスコンストラクターで既に設定されているにもかかわらず、スーパークラスのフィールドの値を設定します。xycolorradius

基本的に、現時点では少し混乱しています。特定のインスタンスではなく、タイプに関連付けられている情報(この場合は静的フィールドである必要があります)と、個々のインスタンスに関連付けられている情報(この場合はインスタンスフィールドである必要があります)を理解する必要があります。サブクラスとスーパークラスで同じフィールド名を使用することはほとんどありません。個人的には、すべてのフィールドをプライベートにすることをお勧めします(定数を除く)。

最後に、なぜコンストラクターはまったくStarRed取りたくないのでしょうか?Colorいつも赤くてはいけませんか?

于 2012-12-05T11:47:07.830 に答える
5

静的変数の色を上書きしています。

staticキーワードは、クラスのすべてのインスタンスが同じ色であることを意味します。

したがって、親と子は同じ静的変数を参照しています。

したがって、後で子の色を設定するため、子の色のみが機能します。

コードを変更して静的を削除する

于 2012-12-05T11:46:41.133 に答える
1

他の回答が言うように、最初に から静的を削除しますpublic static Color colorStarまた、StarRedクラスのフィールドを再宣言する必要はありません。あなたの声明から、赤の色調を決定するRandom red = new Random()ためにいくつかの計算を行いたいと思うので、色の設定を省略した別の保護された Star コンストラクターを追加します。StarRedにそれを使用しますStarRed。の公開コンストラクターもStarそれを使用しますが、さらに星の色を設定します。

コードは次のようになります。

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 コンストラクターから color 変数を削除すると、コンストラクターがオーバーロードStarされます。

お役に立てば幸いです:)

于 2012-12-05T12:06:59.500 に答える