【问题标题】:java cannot find symbol variable superjava找不到符号变量super
【发布时间】: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


【解决方案1】:

FlyingDragon的方法setType改为:

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

【讨论】:

  • 试过了,还是不行,谢谢你的意见!
  • 您还有什么问题?
  • 完全一样的错误,找不到符号变量super
  • 尝试将这两个类放在不同的.java文件中
【解决方案2】:

试试这个:

为实体类制作单独的文件,将所有实体代码放入该类中。

实体类:

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 创建单独的类。

飞龙班

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

【讨论】:

  • 无法编译的源代码 - 错误的树类型,这就是我得到的错误
  • 您使用的是哪个 IDE?
  • 确保当你使用他的源代码时,你也会在他的测试类中获得public class Test {。它不在正常的代码部分,所以你可能错过了。
  • 我在网豆上
猜你喜欢
  • 2016-08-08
  • 1970-01-01
  • 2015-05-13
  • 2012-11-15
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2015-09-14
  • 2016-07-01
相关资源
最近更新 更多