2015-10-16 147 views
0

因此,這裏是我的Superhero類:不明白爲什麼我得到空

public class Superhero { 

    public int strength; 
    public int powerUp; 
    public int defaultStrength = 10; 
    public String name; 

    public Superhero(String name) { 
    this.strength = 10; 
    System.out.println("The Superheroes available are :" + name); 
    } 

    public Superhero(String name, int strength) { 
    if (strength >= 0) { 
     this.strength = strength; 
     System.out.println("The Superheroes available are :" + name); 
    } else { 
     System.out.println("Error. Strength cannot be < 0"); 
    } 
    } 

    public void setStrength(int strength) {   
    this.strength = strength; 
    } 

    public int getStrength() { 
    return strength; 
    } 

    public void powerUp(int powerUp) { 
    this.strength += powerUp; 
    } 

} 

這裏是我Fight類的問題,這裏,當我運行它,我回來了贏家的結果是null和我不不明白爲什麼這樣做。

import java.io.*; 

public class Fight { 

    public static void main (String args[]) { 

    Superhero gambit = new Superhero("Gambit"); 

    Superhero groot = new Superhero("Groot", 79); 

    System.out.println("Gambit's strength is: " + gambit.strength); 
    System.out.println("Groot's strength is: " + groot.strength); 
    System.out.println("The winner of the fight is: " + fight(gambit, groot)); 

    } 

    static String fight(Superhero a, Superhero b) 
    { 
    if (a.strength > b.strength) 
    { 
     return a.name; 
    } else 
    { 
     return b.name; 
    } 
    } 
} 

回答

3

的問題是在你的構造函數:

public Superhero(String name) { 
    this.strength = 10; 
    System.out.println("The Superheroes available are :" + name); 
} 

public Superhero(String name, int strength) { 
    if (strength >= 0) { 
     this.strength = strength; 
     System.out.println("The Superheroes available are :" + name); 
    } else { 
     System.out.println("Error. Strength cannot be < 0"); 
    } 
} 

你的構造函數有一個String name的說法,但你永遠不實例變量設置爲一個值。包含對象的未初始化變量的默認值爲null

2

您永遠不會將「傳入」名稱分配給您班級的屬性字段「名稱」。所以後者保持爲空。

順便說一句:你真的想仔細閱讀這些異常痕跡。他們提供了您需要掌握的所有信息,以便了解正在發生的事情。

最後說明:考慮使用關鍵字final作爲您班級的屬性。這樣你就不會遇到這個問題。因爲編譯器會告訴你,字段「name」未在代碼中初始化。

7

看看你的構造:

public Superhero(String name) { 
    this.strength = 10; 
    System.out.println("The Superheroes available are :" + name); 
} 

,用於設置實例字段strength,但沒有做任何name實例字段。你的其他構造函數是一樣的。您需要包括:

this.name = name; 

將參數中的值複製到實例變量中。在兩個構造函數中都這樣做。否則,您只需以默認值name,這是一個空引用。另外,我強烈建議您將字段設置爲私人,並添加一個getName()方法以從其他fight方法中檢索名稱。我會拋出一個異常,而不是隻是打印出一個錯誤消息,如果強度低於0,我會讓構造函數不參與strength參數只是鏈接到一個:

public Superhero(String name) { 
    this(name, 10); 
} 

public Superhero(String name, int strength) { 
    if (strength < 0) { 
     throw new IllegalArgumentException("strength cannot be negative"); 
    } 
    this.strength = strength; 
    this.name = name; 
    System.out.println("The Superheroes available are :" + name); 
} 

(由構造顯示的消息是有點奇怪,因爲它只是列出一個名字,但是這是一個不同的問題。)

3

你從來沒有在任何的構造函數設置的名稱超級英雄。要解決第一個構造函數,例如:

public Superhero(String name) { 
    this.name = name; 
    this.strength = 10; 
    System.out.println("The Superheroes available are :" + name); 
} 
1

請在您的超級英雄類返回字符串名稱的getter方法,然後調用你的戰鬥類內的方法。我還建議將你的超級英雄類中的全局變量從公共變爲私人,所以他們只能在該類中訪問。

編輯:正如在另一個答案中所述,您的名稱作爲參數的構造函數永遠不會分配給一個變量。

1

你是不是在構造函數中設置名稱變量的值,用這個

public Superhero(String name) { 
    this.strength = 10; 
    this.name = name; 
    System.out.println("The Superheroes available are :" + name); 
} 

public Superhero(String name, int strength) { 

    this.name = name; 
    if (strength >= 0) { 
     this.strength = strength; 
     System.out.println("The Superheroes available are :" + name); 
    } else { 
     System.out.println("Error. Strength cannot be < 0"); 
    } 
} 
相關問題