ネストされた子静的クラスのフィールド値を取得しようとすると、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