繁体   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