简体   繁体   English

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

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

I've been trying to learn Java and I'm working on a project. 我一直在努力学习Java,我正在研究一个项目。

I have an abstract class called Card that I'm using as a template to make sub-classes. 我有一个名为Card的抽象类,我将其用作模板来创建子类。

It has a method to set the name field and color field of the card and a method to set the cost. 它有一种设置卡的名称字段和颜色字段的方法以及设置成本的方法。

I have a subclass of Card called Workshop. 我有一个名为Workshop的卡子类。

I'm trying to use the methods of Card in Workshop to set the name and color fields of Workshop. 我正在尝试使用Workshop in Workshop的方法来设置Workshop的名称和颜色字段。

To do so, I'm calling the methods super.setCardNameColor("Workshop", "Green") and super.setCardCost(0,0,0,0,0,0,0,0); 为此,我调用方法super.setCardNameColor("Workshop", "Green")super.setCardCost(0,0,0,0,0,0,0,0);

However, in order to do this Eclipse in making me use (what appears to be) double curly braces in the class declaration. 但是,为了做到这一点,Eclipse让我在类声明中使用(似乎是)双花括号。

I suspect this has something to do with Anonymous Inner Classes, perhaps from calling 'super' or something like that, but none of my Google searches are giving me the information I need. 我怀疑这与匿名内部课程有关,可能来自“超级”或类似的东西,但我的谷歌搜索都没有给我提供我需要的信息。 Can anyone shed some light on the subject here? 任何人都可以在这里阐述一下这个问题吗? I'm using sysout and getters to make sure that the values are being set correctly on an instance of Workshop, but I'm baffled by the double braces. 我正在使用sysout和getters来确保在Workshop的一个实例上正确设置了值,但是我对双括号感到困惑。 Thank you ahead of time for your help! 提前感谢您的帮助!

Edit: here is the code 编辑:这是代码

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

The inner braces in the "double curly braces" are initializer blocks. “双花括号”中的内括号是初始化块。 (Think of them as a constructor.) (把它们想象成一个构造函数。)

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

See for instance What is an initialization block? 请参阅例如什么是初始化块? .

Since you can't put statements ("code") directly in a class declaration, like this: 因为你不能将语句(“代码”)直接放在类声明中,如下所示:

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

you experience that "Eclipse makes you use double curly braces" because the following 你体验到“Eclipse让你使用双花括号”,因为以下内容

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

makes the statement go in the initializer block with is valid code. 使语句在初始化程序块中使用有效代码。


Regarding your edit: Your problem is here: 关于你的编辑:你的问题在这里:

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

This isn't very common style of programming. 这不是很常见的编程风格。 Perhaps you're after: 也许你是在追求:

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