繁体   English   中英

我在子班上有关于super()的问题

[英]I have a problem at child class about super()

这是我的孩子班:

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

人们总是说“找不到象征性的'超级'”。

您的超类需要具有与您要调用的签名匹配的构造函数。 既然你有

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

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

public Alien(YCoordination currentLocation)

当你说
超级(currentLocation); 您使用单个参数currentLocation调用超类(父类)构造函数。 但是您的超类Alien没有一个接受单个参数currentLocation的构造函数。

在这种情况下, Cannot find symbol super意味着:您正在尝试使用单个参数调用超类构造函数,但是我无法在单个类中使用单个currentLocation参数在超类中找到构造函数。

您可能想调用super(currentLocation, energyCanister, lifePoints)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM