Java类声明(Eclipse)中的双重大括号?

时间:2015-02-15 21:26:18

标签: java eclipse

我一直在努力学习Java,而我正在开展一个项目。

我有一个名为Card的抽象类,我将其用作模板来创建子类。

它有一种方法来设置卡的名称字段和颜色字段以及设置成本的方法。

我有一个名为Workshop的卡子类。

我正在尝试使用Workshop in Workshop的方法来设置Workshop的名称和颜色字段。

为此,我正在调用方法super.setCardNameColor("Workshop", "Green")super.setCardCost(0,0,0,0,0,0,0,0);

然而,为了做到这一点,Eclipse让我在类声明中使用(似乎是)双花括号。

我怀疑这与Anonymous Inner Classes有关,可能来自“超级”或类似的东西,但我的Google搜索都没有给我提供我需要的信息。任何人都可以在这里阐述一下这个问题吗?我正在使用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);
    }
}

1 个答案:

答案 0 :(得分:12)

"双花括号中的内括号"是初始化块。 (把它们想象成一个构造函数。)

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

例如参见What is an initialization block?

因为您不能将语句(&#34; code&#34;)直接放在类声明中,如下所示:

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

你体验到了#34; Eclipse让你使用双花括号&#34;因为以下

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