Arraylist.get 返回哈希码而不是类的字段



我正在使用Arraylist作为'英雄' - 基类,战士,法师是派生类。我想通过使用"get"方法返回每个派生类的 lifePoints 和 attackPoints,相反,我得到了这样的东西(我相信它是类的哈希代码(。

注意:我已经通过调试检查了 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;
    }
}

对象的默认toString()就是你所说的哈希码。 为了获得你想要的输出,你需要在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()永远不应该被覆盖,只是在游戏需要特定文本的特殊情况下。

最新更新