尝试获取子嵌套静态类的字段值时,获取null
. *1 *每个类文件由水平线划分。如何检索子战士的值?
abstract class GameCahracter{
public String name;
public String type;
public Weapon weapon;
public int hitPoints;
public String getDescription(){
return type + "; " + name + "; " + hitPoints + " hp; " + weapon.type;
}
public static class Warrior extends Player{
public final String type = "Warrior";
public int hitPoints = 100;
public static final Weapon.Sword weapon = new Weapon.Sword();
}
}
abstract class Player extends GameCahracter {
}
GameCahracter.Warrior wr = new GameCahracter.Warrior();
wr.name = "Joe";
System.out.println( wr.getDescription());
输出:
null; Joe; 0 hp; null