我一直在尝试学习 Java,并且正在从事一个项目。
我有一个名为 Card 的抽象类,我将其用作创建子类的模板。
它有一个方法来设置卡片的名称字段和颜色字段以及一个设置成本的方法。
我有一个名为 Workshop 的 Card 子类。
我正在尝试使用Card in Workshop的方法来设置Workshop的名称和颜色字段。
为此,我调用方法 super.setCardNameColor("Workshop", "Green")
和 super.setCardCost(0,0,0,0,0, 0,0,0);
但是,为了做到这一点,Eclipse 让我在类声明中使用(看起来是)双花括号。
我怀疑这与匿名内部类有关,可能与调用“super”或类似的东西有关,但我的 Google 搜索都没有提供我需要的信息。任何人都可以在这里阐明这个主题吗?我正在使用 sysout 和 getter 来确保在 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);
}
}
最佳答案
“双花括号”中的内括号是初始化 block 。 (将它们视为构造函数。)
<class name> { // class declaration
{ // initializer block
...
}
}
例如参见 What is an initialization block? .
因为你不能把语句(“代码”)直接放在类声明中,像这样:
new YourClass() { System.out.println("hello"); }
您体验到“Eclipse 让您使用双花括号”,因为以下原因
new YourClass() {{ System.out.println("hello"); }}
使语句进入带有有效代码的初始化程序 block 。
关于您的编辑:您的问题在这里:
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);
}
}
关于java - Java 类声明(Eclipse)中的双花括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28531355/