java - 我在 child 类遇到关于 super() 的问题

标签 java inheritance

这是我的 child 类(class):

public class User extends Alien
{

protected XYCoordination currentLocation;
protected int energyCanister;
protected int lifePoints;

public User (XYCoordination currentLocation, int energyCanister, int lifePoints)
{
    super(currentLocation);
}
public int collectCanister()
{
    super.collectCanister();
    return energyCanister;
}
public int caculateLifePoints(int lifePoints)
{
    super.caculateLifePoints(lifePoints);
    return lifePoints;
}
}

这是我的父类:

public class Alien
{
protected XYCoordination currentLocation;
protected Planet currentPlanet;
protected int energyCanister;
protected int lifePoints;
protected int n;

public Alien(XYCoordination currentLocation, int energyCanister, int lifePoints)
{
    this.currentLocation = currentLocation;
    this.energyCanister = energyCanister;
    this.lifePoints = lifePoints;
}

public XYCoordination moveRight(XYCoordination toRight)
{
    currentLocation = currentLocation.addXToRight(toRight);
    return currentLocation;
}
public XYCoordination moveUp(XYCoordination toUp)
{
    currentLocation = currentLocation.addYToUp(toUp);
    return currentLocation;
}
public XYCoordination moveLeft(XYCoordination toLeft)
{
    currentLocation = currentLocation.addXToLeft(toLeft);
    return currentLocation;
}
public XYCoordination moveDown(XYCoordination toDown)
{
    currentLocation = currentLocation.addYToDown(toDown);
    return currentLocation;
}
public int collectCanister()
{
    energyCanister = energyCanister + (int)(n*currentPlanet.getRemainingCanister());
    return energyCanister;
}
public int caculateLifePoints(int lifePoints)
{
    lifePoints = (int)((2/3)*lifePoints);
    return lifePoints;
}
public void displayInfo()
{
    System.out.print("Currently you are in:" + currentLocation + "\t Currently you have" + energyCanister + "canisters" + "\tCurrently you have" + lifePoints + "lifepoints");
}
}

总是说“找不到‘ super ’符号。”

最佳答案

您的父类(super class)需要有一个与您调用的签名相匹配的构造函数。既然你有

public User (XYCoordination currentLocation, ....) {
    super(currentLocation);
}

您要扩展的 Alien 类需要有一个具有匹配签名的构造函数 -

public Alien(YCoordination currentLocation)

关于java - 我在 child 类遇到关于 super() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4362183/

相关文章:

java - MongoDB java 正则表达式不适用于数组中嵌套的映射

java - 不清楚 "fromIndex"中 "indexOf(int ch, int fromIndex)"的使用

java - 继承GAME的Api方法

django - django 中多个继承类的反比关系

c++ - 类继承 C++

java - 如何通过node.js中的套接字连接发送true

具有动态 URL 的 Java android webview

java - 如何调用 parking 票(parker, parker)?

c++ - 使用 "using"语法将标准容器成员方法引入其子类的范围对运算符不起作用

Swift继承函数返回类型不能是子类型