簡體   English   中英

覆蓋java中的構造函數

[英]override constructor in java

你好,我有一個用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));
    }

}

第一個問題:

protected static Color color;

這意味着該字段(您有兩個......)在整個類型中共享。 我原以為這是一個實例字段,因此不同的顏色可以是不同的顏色。 相反,所有的星星都是相同的顏色,除非你在StarRed有一些使用color域的代碼,在這種情況下你可能有兩種顏色的星......但它仍然不對。

第二個問題:你的StarRed類為xycolor聲明了自己的字段,盡管它們也在超類中聲明。 然后,您將設置超類的radius字段的值,盡管已經在超類構造函數中設置了該值。

基本上它現在有點困惑。 您應該計算出與類型相關聯的信息而不是任何特定實例(在這種情況下應該是靜態字段)以及與各個實例相關聯的信息(在這種情況下,這些實例應該是實例字段)。 你幾乎應該在子類和超類中使用相同的字段名稱 - 而且我個人建議將所有字段設為私有(除了常量之外)。

最后,為什么StarRed構造函數想要獲取Color 它不應該永遠是紅色的嗎?

您正在覆蓋靜態變量顏色。

靜態關鍵字表示該類的所有實例都具有相同的顏色。

所以父和子指的是同一個靜態變量。

因此,因為您在以后設置子顏色只有子顏色工作。

更改您的代碼並刪除靜態

正如其他答案所說,首先從public static Color color刪除public static Color color 此外,您無需在StarRed類中重新聲明Star的字段。 Random red = new Random()語句中我假設您想在StarRed進行一些計算以確定紅色調,因此您添加了另一個受保護的Star構造函數,它忽略了設置顏色。 您將其用於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構造函數中刪除顏色變量,那么您只需重載 Star構造函數。

我希望它有幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM