2017-05-17 101 views
0

我使用Arraylist作爲'英雄' - 基類,而戰士,法師是派生類。我想通過使用'get'方法返回每個派生類的lifePoints和attackPoints,而我得到這樣的東西(我相信它是類的hashCode)。 (i)通過調試,它顯示正確的值,但我不知道如何返回它們,所以我想到了沒有參數的構造函數 - 失敗了。Arraylist.get返回哈希代碼,而不是班級的字段

輸出:

Hero 0 is a [email protected] 
Hero 1 is a [email protected] 
Hero 2 is a [email protected] 
Hero 3 is a [email protected] 

預期輸出:

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

這裏是所有代碼:

Hero-

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

這是沒有toString的類的默認字符串轉換。創建一個toString或者明確地打印出來。每個人都有優點和缺點。 –

+1

考慮重寫'toString' – bradimus

+0

@DaveNewton Works。我真笨。謝謝! –

回答

0

對象的默認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()不應該被覆蓋,只是不是在這種特殊情況下,具體文本是必要的遊戲。

+0

我不允許更改主代碼。我只需重寫toString就可以返回剛剛在getDesc func中寫的內容。謝謝史蒂夫 –

+0

「我不會重寫'toString()'那麼你面對已確立的最佳實踐飛行。應該總是爲值類型重寫'toString',並且它應該嵌入'equals'中涉及的所有字段的表示。避免這種情況的建議是錯誤的。 –

+0

爲了澄清,我的意思是我不會爲了這個特殊用途而重寫它(這大概是針對遊戲的一個特定功能,並不包括所有字段)。是的,'toString()'應該被覆蓋,但是出於調試的目的,正如你所描述的。 –