我被要求这样做:
设计并实现一个名为 MonetaryCoin 的类,该类派生自第 5 章中介绍的 Coin 类。在货币硬币中存储一个表示其价值的值,并为货币价值添加 getter 和 setter 方法。
Coin 类如下:
public class Coin
{
public final int HEADS = 0;
public final int TAILS = 1;
private int face;
// ---------------------------------------------
// Sets up the coin by flipping it initially.
// ---------------------------------------------
public Coin ()
{
flip();
}
// -----------------------------------------------
// Flips the coin by randomly choosing a face.
// -----------------------------------------------
public void flip()
{
face = (int) (Math.random() * 2);
}
// ---------------------------------------------------------
// Returns true if the current face of the coin is heads.
// ---------------------------------------------------------
public boolean isHeads()
{
return (face == HEADS);
}
// ----------------------------------------------------
// Returns the current face of the coin as a string.
// ----------------------------------------------------
public String toString()
{
String faceName;
if (face == HEADS)
faceName = "Heads";
else
faceName = "Tails";
return faceName;
}
}
我想出了这个:
public class MonetaryCoinHW extends Coin
{
public MonetaryCoinHW(int face)
{
setFace(face);
}
public int getFace()
{
if (isHeads()) {
return HEADS;
}
return TAILS;
}
public void setFace( int newFace )
{
while (newFace != getFace()) {
flip();
}
}
但是,我不断收到语法错误......我没有正确使用“超级”吗?我完全糊涂了;我的错误是什么?