java找不到符号变量super

时间:2017-04-05 04:25:30

标签: java inheritance override super cannot-find-symbol

你好,我超级卡住,无法解决为什么这不起作用。我试图使用继承和覆盖,但我不断收到此错误。一直试图在过去一小时内解决这个问题,但一直没有任何线索。我可能会遗漏一些愚蠢的东西,任何帮助都会非常感激。

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

2 个答案:

答案 0 :(得分:0)

setType的方法FlyingDragon更改为:

public void setType(String type) {
        super.setType("Flying Dragon");
    }

答案 1 :(得分:0)

试试这个:

为Entity类创建单独的文件,将所有实体代码放入此类。

实体类:

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

现在为FlyingDragon创建单独的类。

FlyingDragon Class

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 class Test {

    public static void main(String[] args)  {

        FlyingDragon d = new FlyingDragon(1,2,3);

        //whatever you want to do here
    }
}