我一直在努力学习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);
}
}
Run Code Online (Sandbox Code Playgroud)
aio*_*obe 12
"双花括号"中的内括号是初始化块.(把它们想象成一个构造函数.)
<class name> { // class declaration
{ // initializer block
...
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅例如什么是初始化块?.
因为你不能将语句("代码")直接放在类声明中,如下所示:
new YourClass() { System.out.println("hello"); }
Run Code Online (Sandbox Code Playgroud)
你体验到"Eclipse让你使用双花括号"因为以下内容
new YourClass() {{ System.out.println("hello"); }}
Run Code Online (Sandbox Code Playgroud)
使语句在初始化程序块中使用有效代码.
关于你的编辑:你的问题在这里:
public class Workshop extends Card {{
super.setCardNameColor("Workshop", "Green");
super.setCardCost(0, 0, 0, 0, 0, 1, 0, 0);
}}
Run Code Online (Sandbox Code Playgroud)
这不是很常见的编程风格.也许你是在追求:
public class Workshop extends Card {
public Workshop() {
setCardNameColor("Workshop", "Green");
setCardCost(0, 0, 0, 0, 0, 1, 0, 0);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
461 次 |
| 最近记录: |