开发者

I have some problem with child class that how to use the super.method()

my child class is

public class User extends Alien
{

public User(XYCoordination currentLocation, int energyCanister, int lifePoints, String name)

{

super(currentLocation, energyCanister,lifePoints, name);

}

    public int collectCanister(NormalPlanet canister)
    {
        super.collectCanister();
        return energyCanister;
    }

}

my parent class is:

public class Alien
{

protected XYCoordination currentLocation;

protected Planet currentPlanet;

 protected int energyCanister;

 protected int lifePoints;

protec开发者_运维知识库ted int n;

 private String name;

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

...

    public int collectCanister(NormalPlanet canister)
    {
        energyCanister = energyCanister + (int)(n*canister.getRemainingCanister());
        return energyCanister;
    }
...

}

when I compile it, the child class with

public int collectCanister(NormalPlanet canister)
{
    super.collectCanister();
    return energyCanister;
}

is not working? What can i do?


First, your super.collectCanister(...) takes an argument and second the constructor for Alien should take two more arguments - otherwise both lifePoints and name will not be set!

This is your "new" call to super.collectCanister method:

public int collectCanister(NormalPlanet canister)
{
    super.collectCanister(canister);
    return energyCanister;
}

And this is how your constructor for alien should look like:

public Alien(XYCoordination currentLocation, 
             int energyCanister, 
             int lifePoints, 
             String name) {
    ....
}


public User(XYCoordination currentLocation, int energyCanister, int lifePoints, String name)  {
   super(currentLocation, energyCanister,lifePoints, name);
}

I can't see how this will work? The super does not contain four parameters, only currentLocation, and eneryCanister.

You need to change the signature of your Alien constructor to be

public Alien(XYCoordination currentLocation, int energyCanister, int lifePoints, String name)


Your SuperKonstructor takes only two arguments:

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

and not four as you do it in your subclass. And additionally in your super constructor you assign name and lifePoints to fields of this class, but lifePoinbts and name aren't constructor arguments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜