gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:20:49 25 4
gpt4 key购买 nike

这是我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com