java找不到符号变量super
你好,我被卡住了,无法理解为什么这不起作用。我正在尝试使用继承和重写,但我一直遇到这个错误。在过去的一个小时里,我一直在想这个问题,但是没有任何线索。我可能错过了一些愚蠢的事情,任何帮助都将不胜感激java找不到符号变量super,java,inheritance,overriding,super,cannot-find-symbol,Java,Inheritance,Overriding,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 voi
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;
}
}
将
FlyingDragon
的方法setType
更改为:
public void setType(String type) {
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;
}
}
public class Test {
public static void main(String[] args) {
FlyingDragon d = new FlyingDragon(1,2,3);
//whatever you want to do here
}
}
现在为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 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;
}
}
public class Test {
public static void main(String[] args) {
FlyingDragon d = new FlyingDragon(1,2,3);
//whatever you want to do here
}
}
您将setter定义为
setType
,但使用了type
。仍然是相同的错误,但感谢您的建议为什么要重写setter?假设公共抽象类实体{}
最后缺少一个}。是的,很抱歉它不在我的程序中,再次感谢您的输入,但仍然没有工作,谢谢你的意见!还有什么问题吗?完全相同的错误,找不到符号变量super请尝试将这两个类放在单独的中。java文件不可编译源代码-错误的树类型,这就是我在使用哪个IDE时遇到的错误?请确保在使用其源代码时,也获得了公共类测试{
在他的测试类中。它不在正常代码部分,因此您可能错过了netbean上的.im