LoginSignup
0
0

More than 5 years have passed since last update.

JAVA-コンストラクタで初期値設定(P350)

Last updated at Posted at 2015-10-23

JAVAでは「インスタンスが生まれた直後に自動実行される処理」を
プログラミング出来るようになっています。
▪️Main2.java

public class Main2 {
    public static void main(String[] args) {
        Hero h1 = new Hero();
        h1.name = "ミナト";
        h1.hp = 3;
        Hero h2 = new Hero();
        h2.name = "アサカ";
        h2.hp = 3;
        Wizard w = new Wizard();
        w.name = "スガワラ";
        w.hp = 50;
        w.heal(h1);
        w.heal(h2);
        System.out.println(h1.hp);
        System.out.println(h2.hp);

        Matango m1 = new Matango();
        m1.hp = 50;
        m1.suffix = 'A';

        Matango m2 = new Matango();
        m2.hp = 48;
        m2.suffix = 'B';
    }

}

▪️Hero.java


public class Hero {
    String name;
    int hp;
    Sword sword;

    void attack() {
        System.out.println(this.name + "は攻撃した!");
        System.out.println("敵に5ポイントのダメージを与えた!");
    }

    void sleep() {
        this.hp = 100;
        System.out.println(this.name + "は、眠って回復した!");

    }

    void sit(int sec) {
        this.hp += sec;
        System.out.println(this.name + "は、" + sec + "秒座った!");
        System.out.println("HPが" + sec + "ポイント回復した.");

    }

    void slip() {
        this.hp -= 5;
        System.out.println(this.name + "は、転んだ!");
        System.out.println("5のダメージ!");

    }

    void run() {
        System.out.println(this.name + "は、逃げ出した!");
        System.out.println("GAMEOVER");
        System.out.println("最終HPは" + this.hp + "でした");

    }

    Hero() {
        this.hp = 100;
        this.name = "ねこ";
    }
}

▪️Wizard.java


public class Wizard {
    String name;
    int hp;

    void heal(Hero h) {
        h.hp += 2;
        System.out.println(h.name + "のHPを10回復した!");

    }

}

▪️実行結果
ミナトのHPを10回復した!
アサカのHPを10回復した!
5
5

▪️解説

Hero() {
        this.hp = 100;
        this.name = "ねこ";
    }

this.hp = 100;
としても、mainメソッドに

Hero h1 = new Hero();
h1.hp = 3;

とすると、
Hero h1 = new Hero();のHero()を呼び出して、
Hero()というメソッドでせっかくthis.hp = 100;で初期値100としても
h1.hp = 3;としてるから前のHero()というメソッドで設定した初期値を無視して
結果、h1.hp = 3;となる。

Hero()メソッドはコンストラクタ

Hero()メソッドはnewされた直後に自動的に実行されるという特別な性質を持っています。

▪️Main2.javaコンストラクタで初期値設定

public class Main2 {
    public static void main(String[] args) {
        Hero h1 = new Hero();
        Hero h2 = new Hero();
        Wizard w = new Wizard();
        w.name = "スガワラ";
        w.hp = 50;
        w.heal(h1);
        w.heal(h2);
        System.out.println(h1.hp);
        System.out.println(h2.hp);

        Matango m1 = new Matango();
        m1.hp = 50;
        m1.suffix = 'A';

        Matango m2 = new Matango();
        m2.hp = 48;
        m2.suffix = 'B';

    }

}

▪️Hero.javaコンストラクタで初期値設定


public class Hero {
    String name;
    int hp;
    Sword sword;

    void attack() {
        System.out.println(this.name + "は攻撃した!");
        System.out.println("敵に5ポイントのダメージを与えた!");
    }

    void sleep() {
        this.hp = 100;
        System.out.println(this.name + "は、眠って回復した!");

    }

    void sit(int sec) {
        this.hp += sec;
        System.out.println(this.name + "は、" + sec + "秒座った!");
        System.out.println("HPが" + sec + "ポイント回復した.");

    }

    void slip() {
        this.hp -= 5;
        System.out.println(this.name + "は、転んだ!");
        System.out.println("5のダメージ!");

    }

    void run() {
        System.out.println(this.name + "は、逃げ出した!");
        System.out.println("GAMEOVER");
        System.out.println("最終HPは" + this.hp + "でした");

    }

    Hero() {
        this.hp = 100;
        this.name = "ねこ";
    }
}

▪️Withard.javaコンストラクタで初期値設定


public class Wizard {
    String name;
    int hp;

    void heal(Hero h) {
        h.hp += 2;
        System.out.println(h.name + "のHPを10回復した!");

    }

}

▪️実行結果コンストラクタで初期値設定
ねこのHPを10回復した!
ねこのHPを10回復した!
102
102

▪️Hero.javaは、これでも初期値設定できる
フィールド宣言による初期値の定義

public class Hero {
    String name = "ねこ";
    int hp = 100;
    Sword sword;

    void attack() {
        System.out.println(this.name + "は攻撃した!");
        System.out.println("敵に5ポイントのダメージを与えた!");
    }

    void sleep() {
        this.hp = 100;
        System.out.println(this.name + "は、眠って回復した!");

    }

    void sit(int sec) {
        this.hp += sec;
        System.out.println(this.name + "は、" + sec + "秒座った!");
        System.out.println("HPが" + sec + "ポイント回復した.");

    }

    void slip() {
        this.hp -= 5;
        System.out.println(this.name + "は、転んだ!");
        System.out.println("5のダメージ!");

    }

    void run() {
        System.out.println(this.name + "は、逃げ出した!");
        System.out.println("GAMEOVER");
        System.out.println("最終HPは" + this.hp + "でした");

    }

}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0