コンテンツにスキップ

第10章

コード10-01

Hero.java

public class Hero {
  String name = "ミナト";
  int hp = 100;
  // 戦う
  public void attack(Matango m) {
    System.out.println(this.name + "の攻撃!");
    m.hp -= 5;
    System.out.println("5ポイントのダメージをあたえた!");
  }
  // 逃げる
  public void run() {
    System.out.println(this.name + "は逃げ出した!");
  }
}

Matango.java

public class Matango {
  int hp;
  final int LEVEL = 10;
  char suffix;
  public void run() {
    System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
  }
}

コード10-02

Matango.java

public class Matango {
  int hp;
  final int LEVEL = 10;
  char suffix;
  public void run() {
    System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
  }
}

SuperHero.java

public class SuperHero {
  String name = "ミナト";
  int hp = 100;
  boolean flying;
  // 戦う
  public void attack(Matango m) {
    System.out.println(this.name + "の攻撃!");
    m.hp -= 5;
    System.out.println("5ポイントのダメージをあたえた!");
  }
  // 逃げる
  public void run() {
    System.out.println(this.name + "は逃げ出した!");
  }
  // 飛ぶ
  public void fly() {
    this.flying = true;
    System.out.println("飛び上がった!");
  }
  // 着地する
  public void land() {
    this.flying = false;
    System.out.println("着地した!");
  }
}

コード10-03

Hero.java

public class Hero {
  String name = "ミナト";
  int hp = 100;
  // 戦う
  public void attack(Matango m) {
    System.out.println(this.name + "の攻撃!");
    m.hp -= 5;
    System.out.println("5ポイントのダメージをあたえた!");
  }
  // 逃げる
  public void run() {
    System.out.println(this.name + "は逃げ出した!");
  }
}

Matango.java

public class Matango {
  int hp;
  final int LEVEL = 10;
  char suffix;
  public void run() {
    System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
  }
}

SuperHero.java

public class SuperHero extends Hero {
  boolean flying;
  public void fly() {
    this.flying = true;
    System.out.println("飛び上がった!");
  }
  public void land() {
    this.flying = false;
    System.out.println("着地した!");
  }
}

コード10-04

Hero.java

public class Hero {
  String name = "ミナト";
  int hp = 100;
  // 戦う
  public void attack(Matango m) {
    System.out.println(this.name + "の攻撃!");
    m.hp -= 5;
    System.out.println("5ポイントのダメージをあたえた!");
  }
  // 逃げる
  public void run() {
    System.out.println(this.name + "は逃げ出した!");
  }
}

Main.java

public class Main {
  public static void main(String[] args) {
    SuperHero sh = new SuperHero();
    sh.run();
  }
}

Matango.java

public class Matango {
  int hp;
  final int LEVEL = 10;
  char suffix;
  public void run() {
    System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
  }
}

SuperHero.java

public class SuperHero extends Hero {
  boolean flying;
  public void fly() {
    this.flying = true;
    System.out.println("飛び上がった!");
  }
  public void land() {
    this.flying = false;
    System.out.println("着地した!");
  }
}

コード10-05

Hero.java

public class Hero {
  String name = "ミナト";
  int hp = 100;
  // 戦う
  public void attack(Matango m) {
    System.out.println(this.name + "の攻撃!");
    m.hp -= 5;
    System.out.println("5ポイントのダメージをあたえた!");
  }
  // 逃げる
  public void run() {
    System.out.println(this.name + "は逃げ出した!");
  }
}

Matango.java

public class Matango {
  int hp;
  final int LEVEL = 10;
  char suffix;
  public void run() {
    System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
  }
}

SuperHero.java

public class SuperHero extends Hero {
  boolean flying;
  public void fly() {
    this.flying = true;
    System.out.println("飛び上がった!");
  }
  public void land(){
    this.flying = false;
    System.out.println("着地した!");
  }
  public void run() {
    System.out.println(this.name + "は撤退した");
  }
}

コード10-06

Hero.java

public class Hero {
  String name = "ミナト";
  int hp = 100;
  // 戦う
  public void attack(Matango m) {
    System.out.println(this.name + "の攻撃!");
    m.hp -= 5;
    System.out.println("5ポイントのダメージをあたえた!");
  }
  // 逃げる
  public void run() {
    System.out.println(this.name + "は逃げ出した!");
  }
}

Main.java

public class Main {
  public static void main(String[] args) {
    Hero h = new Hero();
    h.run();
    SuperHero sh = new SuperHero();
    sh.run();
  }
}

Matango.java

public class Matango {
  int hp;
  final int LEVEL = 10;
  char suffix;
  public void run() {
    System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
  }
}

SuperHero.java

public class SuperHero extends Hero {
  boolean flying;
  public void fly() {
    this.flying = true;
    System.out.println("飛び上がった!");
  }
  public void land() {
    this.flying = false;
    System.out.println("着地した!");
  }
  public void run() {
    System.out.println(this.name + "は撤退した");
  }
}

コード10-07

Hero.java

public class Hero {
  String name = "ミナト";
  int hp = 100;

  public void attack(Matango m) {
    System.out.println(this.name + "の攻撃!");
    m.hp -= 5;
    System.out.println("5ポイントのダメージをあたえた!");
  }
  public final void slip() {
    this.hp -= 5;
    System.out.println(this.name + "は転んだ!");
    System.out.println("5のダメージ");
  }
  public void run() {
    System.out.println(this.name + "は逃げ出した!");
  }
}

Matango.java

public class Matango {
  int hp;
  final int LEVEL = 10;
  char suffix;
  public void run() {
    System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
  }
}

コード10-08

Hero.java

public class Hero {
  String name = "ミナト";
  int hp = 100;

  public void attack(Matango m) {
    System.out.println(this.name + "の攻撃!");
    m.hp -= 5;
    System.out.println("5ポイントのダメージをあたえた!");
  }
  public final void slip() {
    this.hp -= 5;
    System.out.println(this.name + "は転んだ!");
    System.out.println("5のダメージ");
  }
  public void run() {
    System.out.println(this.name + "は逃げ出した!");
  }
}

Matango.java

public class Matango {
  int hp;
  final int LEVEL = 10;
  char suffix;
  public void run() {
    System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
  }
}

SuperHero.java

public class SuperHero extends Hero {
  boolean flying;
  public void fly(){
    this.flying = true;
    System.out.println("飛び上がった!");
  }
  public void land() {
    this.flying = false;
    System.out.println("着地した!");
  }
  public void run() {
    System.out.println("撤退した");
  }
  public void attack(Matango m) {
    System.out.println(this.name + "の攻撃!");
    m.hp -= 5;
    System.out.println("5ポイントのダメージをあたえた!");
    if (this.flying) {
      System.out.println(this.name + "の攻撃!");
      m.hp -= 5;
      System.out.println("5ポイントのダメージをあたえた!");
    }
  }
}

コード10-09

Hero.java

public class Hero {
  String name = "ミナト";
  int hp = 100;

  public void attack(Matango m) {
    System.out.println(this.name + "の攻撃!");
    m.hp -= 5;
    System.out.println("5ポイントのダメージをあたえた!");
  }
  public final void slip() {
    this.hp -= 5;
    System.out.println(this.name + "は転んだ!");
    System.out.println("5のダメージ");
  }
  public void run() {
    System.out.println(this.name + "は逃げ出した!");
  }
}

Matango.java

public class Matango {
  int hp;
  final int LEVEL = 10;
  char suffix;
  public void run() {
    System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
  }
}

SuperHero.java

public class SuperHero extends Hero {
  boolean flying;
  public void fly() {
    this.flying = true;
    System.out.println("飛び上がった!");
  }
  public void land() {
    this.flying = false;
    System.out.println("着地した!");
  }
  public void run() {
    System.out.println("撤退した");
  }
  public void attack(Matango m) {
    super.attack(m);
    if (this.flying) {
      super.attack(m);
    }
  }
}

コード10-10

Hero.java

public class Hero {
  String name = "ミナト";
  int hp = 100;

  public Hero() {
    System.out.println("Heroのコンストラクタが動作");
  }
  public void attack(Matango m) {
    System.out.println(this.name + "の攻撃!");
    m.hp -= 5;
    System.out.println("5ポイントのダメージをあたえた!");
  }
  public final void slip() {
    this.hp -= 5;
    System.out.println(this.name + "は転んだ!");
    System.out.println("5のダメージ");
  }
  public void run() {
    System.out.println(this.name + "は逃げ出した!");
  }
}

Main.java

public class Main {
  public static void main(String[] args) {
    SuperHero sh = new SuperHero();
  }
}

Matango.java

public class Matango {
  int hp;
  final int LEVEL = 10;
  char suffix;
  public void run() {
    System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
  }
}

SuperHero.java

public class SuperHero extends Hero {
  boolean flying;

  public SuperHero() {
    System.out.println("SuperHeroのコンストラクタが動作");
  }
  public void fly() {
    this.flying = true;
    System.out.println("飛び上がった!");
  }
  public void land() {
    this.flying = false;
    System.out.println("着地した!");
  }
  public void run() {
    System.out.println("撤退した");
  }
  public void attack(Matango m) {
    super.attack(m);
    if (this.flying) {
      super.attack(m);
    }
  }
}

コード10-11

Item.java

public class Item {
  String name;
  int price;
  public Item(String name) {
    this.name = name;
    this.price = 0;
  }
  public Item(String name, int price) {
    this.name = name;
    this.price = price;
  }
}

Main.java

public class Main {
  public static void main(String[] args) {
    Weapon w = new Weapon();
  }
}

Weapon.java

public class Weapon extends Item { /* … */ }