コード判定結果
Java入門編8:さらにクラスを理解しよう
#05:RPGのPlayerクラスを継承で記述1
この動画を見るにはpaiza会員登録のうえ
有料会員登録が必要です
ここでは、クラスを継承する具体例として、RPGのPlayerクラスを継承で記述します。
まずは、親クラスを作成しましょう。
// RPGのPlayerクラスを継承で記述 その1
public class Main {
public static void main(String[] args) {
System.out.println("=== パーティでスライムと戦う ===");
Player hero = new Player("勇者");
Player warrior = new Player("戦士");
Player[] party = {hero, warrior};
for (Player member : party) {
member.attack("スライム");
}
// hero.attack("スライム");
}
}
class Player {
public String myName;
public Player(String name) {
myName = name;
}
public void attack(String enemy) {
System.out.println(myName + "は、" + enemy + "を攻撃した!");
}
}