簡體   English   中英

不明白為什么我會收到null

[英]Don't understand why I receive null

這是我的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;
    }
  }
}

看一下你的構造函數:

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

這設置了實例字段的strength ,但對name實例字段沒有任何作用 您的其他構造函數是相同的。 您需要包括:

this.name = name;

將值從參數復制到實例變量。 在兩個構造函數中都執行此操作。 否則,您最終只會得到name的默認值,它是一個空引用。

getName()說一句,我強烈建議您將字段設為私有,並添加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);
}

(構造函數顯示的消息有點奇怪,因為它僅列出一個名稱,但這是另一回事。)

問題出在您的構造函數中:

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

您從未在任何Superhero構造函數中設置名稱。 要修復第一個構造函數,例如:

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

您永遠不會將“傳入”名稱分配給類的屬性字段“名稱”。 因此,后一個保持為空。

順便說一句:您真的想仔細閱讀那些異常痕跡。 他們提供了您需要的所有信息,以了解發生了什么情況。

最后的注意事項:考慮為類的屬性使用關鍵字final。 這樣,您就不會遇到這個問題。 正如編譯器會告訴您的那樣,字段“名稱”未在代碼中初始化。

創建一個getter方法,該方法在您的超級英雄類中返回String名稱,然后在Fight類中調用該方法。 我還建議將您的超級英雄類中的全局變量從公共更改為私有,因此只能在該類中訪問它們。

編輯:如另一個答案所述,以名稱為參數的構造函數永遠不會分配給變量。

您沒有在構造函數中設置name變量的值,請使用此

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM