У меня есть проблема в классе ребенка о супер () - PullRequest
0 голосов
/ 06 декабря 2010

Это мой дочерний класс:

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

Всегда говорят "не могу найти символ" супер "."

Ответы [ 2 ]

4 голосов
/ 06 декабря 2010

Ваш суперкласс должен иметь конструктор, который соответствует подписи, которую вы вызываете. Так как у вас есть

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

классу Alien, который вы расширяете, нужен конструктор с совпадающей сигнатурой

public Alien(YCoordination currentLocation)
0 голосов
/ 06 декабря 2010

Когда вы говорите
super (currentLocation);Вы вызываете конструктор суперкласса (родительского класса) с единственным аргументом currentLocation.Но ваш суперкласс Alien не имеет конструктора, который принимает один аргумент currentLocation.

Cannot find symbol super в этом контексте означает: вы пытаетесь вызвать конструктор суперкласса с одним параметром, но яневозможно найти конструктор в суперклассе с единственным аргументом currentLocation.

Возможно, вы хотели вызвать super(currentLocation, energyCanister, lifePoints)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...