【发布时间】:2017-04-05 04:25:29
【问题描述】:
你好,我被卡住了,无法弄清楚为什么这不起作用。我正在尝试使用继承和覆盖,但我不断收到此错误。在过去的一个小时里一直试图弄清楚这一点,但没有任何线索。我可能遗漏了一些愚蠢的东西,任何帮助将不胜感激。
public class FlyingDragon extends Entity {
private int Hp;
public FlyingDragon(int x, int y, int Hp){
super (x, y);
this.Hp = Hp;
}
public void setHp(int Hp){
this.Hp = 100;
}
public int getHp(){
return Hp;}
public void setType(String type) { ###error is here
super.setType("Flying Dragon");}
}
public abstract class Entity {
private char symbol; // symbol that represents the entity
private String type; // every entity is of a type
private int x; // x coordinate in the room
private int y; // y coordinate in the room
public Entity (int x, int y) {
type = "entity";
this.x=x;
this.y =y;
}
public char getSymbol() {
return symbol;
}
public void setSymbol(char c) {
symbol = c;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX (int newx) {
this.x=newx;
}
public void setY (int newy) {
this.y=newy;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
【问题讨论】:
-
您将 setter 定义为
setType,但使用了type。 -
还是同样的错误,但感谢您的建议
-
为什么要重写setter?
-
假设
public abstract class Entity { }在末尾缺少一个 }。 -
是的,很抱歉它不在我的程序中,再次感谢您的输入
标签: java inheritance overriding super cannot-find-symbol