簡體   English   中英

Java類聲明(Eclipse)中的雙重大括號?

[英]Double Curly Braces in Java Class Declaration (Eclipse)?

我一直在努力學習Java,我正在研究一個項目。

我有一個名為Card的抽象類,我將其用作模板來創建子類。

它有一種設置卡的名稱字段和顏色字段的方法以及設置成本的方法。

我有一個名為Workshop的卡子類。

我正在嘗試使用Workshop in Workshop的方法來設置Workshop的名稱和顏色字段。

為此,我調用方法super.setCardNameColor("Workshop", "Green")super.setCardCost(0,0,0,0,0,0,0,0);

但是,為了做到這一點,Eclipse讓我在類聲明中使用(似乎是)雙花括號。

我懷疑這與匿名內部課程有關,可能來自“超級”或類似的東西,但我的谷歌搜索都沒有給我提供我需要的信息。 任何人都可以在這里闡述一下這個問題嗎? 我正在使用sysout和getters來確保在Workshop的一個實例上正確設置了值,但是我對雙括號感到困惑。 提前感謝您的幫助!

編輯:這是代碼

public abstract class Card
{
    private String cardName = "";
    private String cardColor = "";
    private int coinCost = 0;
    private int woodCost = 0;
    private int brickCost = 0;
    private int stoneCost = 0;
    private int oreCost = 0;
    private int glassCost = 0;
    private int clothCost = 0;
    private int paperCost = 0;

    private int coinValue = 0;
    private int pointValue = 0;
    private int millitaryValue = 0;
    private int woodValue = 0;
    private int brickValue = 0;
    private int stoneValue = 0;
    private int oreValue = 0;
    private int glassValue = 0;
    private int clothValue = 0;
    private int paperValue = 0;

    private Card discountForCard;
    private Card discountFromCard;

    public void setCardNameColor(String cardName, String cardColor) {
        this.cardName = cardName;
        this.cardColor = cardColor;
    }

    public void setCardCost(int coinCost, int woodCost, int brickCost,
            int stoneCost, int orecost, int glassCost, int clothCost,
            int paperCost) {
        this.coinCost = coinCost;
        this.woodCost = woodCost;
        this.brickCost = brickCost;
        this.stoneCost = stoneCost;
        this.oreCost = orecost;
        this.glassCost = glassCost;
        this.clothCost = clothCost;
        this.paperCost = paperCost;
    }

    public void setCardValue(int coinValue, int millitaryValue, int pointValue,
            int woodValue, int brickValue, int stoneValue, int oreValue,
            int glassValue, int clothValue, int paperValue) {
        this.coinValue = coinValue;
        this.millitaryValue = millitaryValue;
        this.pointValue = pointValue;
        this.woodValue = woodValue;
        this.brickValue = brickValue;
        this.stoneValue = stoneValue;
        this.oreValue = oreValue;
        this.glassValue = glassValue;
        this.clothValue = clothValue;
        this.paperValue = paperValue;
    }

    public void getCardInfo() {
        System.out.println(cardName + cardColor + coinCost + woodCost+brickCost+stoneCost+oreCost+glassCost+clothCost+paperCost);
    }

    public void setGivesDiscountTo(Card card) {
        card = discountForCard;
    }

    public void setReceivesDiscountFrom(Card card) {
        card = discountFromCard;
    }

    public String getCardName() {
        return cardName;
    }
}


public class Workshop extends Card
{
    {
        super.setCardNameColor("Workshop", "Green");
        super.setCardCost(0, 0, 0, 0, 0, 1, 0, 0);
    }
}

“雙花括號”中的內括號是初始化塊。 (把它們想象成一個構造函數。)

<class name> {       // class declaration
    {   // initializer block
        ...
    }
}

請參閱例如什么是初始化塊?

因為你不能將語句(“代碼”)直接放在類聲明中,如下所示:

new YourClass() { System.out.println("hello"); }

你體驗到“Eclipse讓你使用雙花括號”,因為以下內容

new YourClass() {{ System.out.println("hello"); }}

使語句在初始化程序塊中使用有效代碼。


關於你的編輯:你的問題在這里:

public class Workshop extends Card {{
    super.setCardNameColor("Workshop", "Green");
    super.setCardCost(0, 0, 0, 0, 0, 1, 0, 0);
}} 

這不是很常見的編程風格。 也許你是在追求:

public class Workshop extends Card {
    public Workshop() {
        setCardNameColor("Workshop", "Green");
        setCardCost(0, 0, 0, 0, 0, 1, 0, 0);
    }
}

暫無
暫無

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

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