2015-10-16 201 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"); 
    } 
} 
相关问题