LoginSignup
2
1

More than 5 years have passed since last update.

JAVA 継承とコンストラクタ(P430)

Last updated at Posted at 2015-10-30

▪️Main222.java

public class Main222 {

    public static void main(String[] args) {
        Hero22 h = new Hero22("ねこ5");

        SuperHero2 sh = new SuperHero2("ヒーロー");
        SuperHero2 sh2 = new SuperHero2("");
        Weapon2 we = new Weapon2();
        SuperHero2 sh3 = new SuperHero2("スーパーヒーロー");
        Item2 i = new Item2("");
        sh.setName("SprHr");

        System.out.println(sh.getName() + "はSuperHeroの名前");
        System.out.println(we.getName() + "はweaponの名前");
        System.out.println("SuperHero、sh3の名前は" + sh3.getName());
        sh3.setName("スーパー3");
        System.out.println("SuperHero、sh3の名前は" + sh3.getName());

    }

}

▪️Hero22.java


public class Hero22 {
    private String name;
    private int hp;
    static int money;
    static int money2;

    Hero22(String name) {
        this.hp = 100;
        this.name = name;
    }

    Hero22() {
        this("ダミー");
        System.out.println("Hero22のコンストラクタが動作");
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        if (name == null) {
            throw new IllegalArgumentException("名前がnullである。処理を中断。");

        }
        if (name.length() <= 1) {
            throw new IllegalArgumentException("名前が短すぎる。処理を中断。");
        }
        if (name.length() >= 8) {
            throw new IllegalArgumentException("名前が長すぎる。処理を中断。");
        }
        this.name = name;
        System.out.println(this.name);
    }

}

▪️Weapon2.java

public class Weapon2 extends Item2  {
    private String name;

    public Weapon2() {
        super("ななしの剣");

    }
}

▪️Item2.java


public class Item2 {
    private String name;
    private int price;

    public Item2(String name) {
        this.name = name;
        this.price = 0;
    }

    public Item2(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return this.name;
    }
}

▪️SuperHero22.java

public class Item2 {
    private String name;
    private int price;

    public Item2(String name) {
        this.name = name;
        this.price = 0;
    }

    public Item2(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return this.name;
    }
}

▪️実行結果
SuperHero2が生成されました
SuperHero2のコンストラクタが動作
SuperHero2が生成されました
SuperHero2のコンストラクタが動作
SuperHero2が生成されました
SuperHero2のコンストラクタが動作
SprHr
SprHrはSuperHeroの名前
ななしの剣はweaponの名前
SuperHero、sh3の名前はスーパーヒーロー
スーパー3
SuperHero、sh3の名前はスーパー3


Main222.javaに
we.setName("兵器");
System.out.println("Weapon、weの名前は" + we.getName());
を追加しました。

▪️Main222.java

public class Main222 {

    public static void main(String[] args) {
        Hero22 h = new Hero22("ねこ5");

        SuperHero2 sh = new SuperHero2("ヒーロー");
        SuperHero2 sh2 = new SuperHero2("");
        Weapon2 we = new Weapon2();
        SuperHero2 sh3 = new SuperHero2("スーパーヒーロー");
        Item2 i = new Item2("");
        sh.setName("SprHr");

        System.out.println(sh.getName() + "はSuperHeroの名前");
        System.out.println(we.getName() + "はweaponの名前");
        System.out.println("SuperHero、sh3の名前は" + sh3.getName());
        sh3.setName("スーパー3");
        System.out.println("SuperHero、sh3の名前は" + sh3.getName());
        we.setName("兵器");
        System.out.println("Weapon、weの名前は" + we.getName());

    }

}

▪️Item2.java


public class Item2 {
    private String name;
    private int price;

    public Item2(String name) {
        this.name = name;
        this.price = 0;
    }

    public Item2(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

▪️Weapon2.java

public class Weapon2 extends Item2 {
    private String name;

    public Weapon2() {
        super("ななしの剣");

    }
}

▪️実行結果
SuperHero2が生成されました
SuperHero2のコンストラクタが動作
SuperHero2が生成されました
SuperHero2のコンストラクタが動作
SuperHero2が生成されました
SuperHero2のコンストラクタが動作
SprHr
SprHrはSuperHeroの名前
ななしの剣はweaponの名前
SuperHero、sh3の名前はスーパーヒーロー
スーパー3
SuperHero、sh3の名前はスーパー3
Weapon、weの名前は兵器

2
1
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
2
1