Arraylist.get返回hashcode而不是class' s字段

时间:2017-05-17 15:44:45

标签: java polymorphism

我使用Arraylist作为英雄' - 基类,战士,法师是派生类。我想通过使用' get'来返回每个派生类的lifePoints和attackPoints。方法,而是我得到这样的东西(我相信它是该类的hashCode)。

注意:我已经检查了heroes.get(i)通过调试,它显示了正确的值,但我不确定如何返回它们,所以我想关于没有参数的构造函数 - 失败了。

输出:

Hero 0 is a Warrior@7852e922
Hero 1 is a Warrior@4e25154f
Hero 2 is a Magician@70dea4e
Hero 3 is a Magician@5c647e05

预期输出:

Hero 0 is a Warrier with -1 lifePoints and 5 attackPoints
Hero 1 is a Warrier with 5 lifePoints and 2 attackPoints
Hero 2 is a Magician with 12 lifePoints and 2 spellPoints
Hero 3 is a Magician with 13 lifePoints and 2 spellPoints

我的主要课程的半码

for (int i=0; i<heroes.size(); ++i) {
System.out.println("Hero "+i+ " is a "+heroes.get(i));
}

我对解决方案的思考过程:使用构造函数 - 失败。

 public Magician()
     {
         System.out.println("Magician with " + this.lifePoints +"life points and " +this.attackPoints +" spell points.");
     }

以下是所有代码:

英雄 -

abstract class Hero {

    protected int lifePoints;
    protected int attackPoints;

    public abstract Hero attack(Hero other);
    public abstract int lifePoints();

}

法师:

public class Magician extends Hero{

    static int count;

     Magician(int lifePoints, int attackPoints)
     {
        this.lifePoints = lifePoints;
        this.attackPoints = attackPoints;
        count++;
     }

     public Magician()
     {
         System.out.println("Magician with " + this.lifePoints +"life points and " +this.attackPoints +" spell points.");
     }
    @Override
    public Hero attack(Hero other) {
        if(other != null)
        {
            if(other instanceof Hero)
            {
                other.lifePoints /= this.attackPoints;
                if(other.lifePoints <= 0)
                {
                    return new Magician(this.lifePoints,this.attackPoints);
                }
            }
            //Attacking ourselves - Error
            if(this.equals(other))
            {
                System.out.println("Hero can't attack itself");
            }
        }
        return null;
    }

    @Override
    public int lifePoints() {
        return this.lifePoints;
    }

    public static int getNoMagician()
    {
        return count;
    }

}

战士:

public class Warrior extends Hero
{
    static int count;

     Warrior(int lifePoints, int attackPoints)
     {
        this.lifePoints = lifePoints;
        this.attackPoints = attackPoints;
        count++;
     }
     public Warrior()
     {
         System.out.println("Warrior with " + this.lifePoints +"life points and " +this.attackPoints +" spell points.");
     }

    @Override
    public Hero attack(Hero other) {
        if(other != null)
        {
            //We're attacking a warrior
            if(other instanceof Warrior){
                ((Warrior)other).lifePoints -= this.attackPoints;

            }
            //We're attacking a magician
            if(other instanceof Magician){
                ((Magician)other).lifePoints -= (this.attackPoints / 2);
                if(((Magician)other).lifePoints <= (this.lifePoints / 2))
                {
                    return new Warrior(this.lifePoints,this.attackPoints);
                }

            }
            //Attacking ourselves - Error
            if(this.equals(other))
            {
                System.out.println("Hero can't attack itself");
            }
        }
        //False
        return null;
    }

    @Override
    public int lifePoints() {
        return this.lifePoints;
    }

    public static int getNoWarrior()
    {
        return count;
    }

}

1 个答案:

答案 0 :(得分:0)

对象的默认toString()是您调用其Hashcode的内容。为了获得您想要的输出,您需要在Hero的每个子类中创建自己的方法,例如在魔术师课上: -

public String getDescription() {
    return "Magician with " + this.lifePoints +" life points and " +this.attackPoints +" spell points.");
}

然后按如下方式调用此方法: -

for (int i=0; i<heroes.size(); ++i) {
    System.out.println("Hero " + i + " is a "+heroes.get(i).getDescription());
}

请注意,我不会为此目的覆盖toString(),因为此方法通常用于调试。如果其他人编辑了您的代码,他们可能会更改toString()输出,而不会意识到它对程序的功能至关重要。

更新:我并不是说永远不应该覆盖toString(),只是不要在游戏需要特定文本的特定情况下。