JAVA 高度な継承(abstract,interface,implements)(練習問題P491(P444~P496))

第12章 高度な継承P444~P496
■Main23.java

```java
public class Main23 {
	public static void main(String[] args) {
		Hero2 h = new Hero2("ねこ５");
		Hero2 h1 = new Hero2("ミナト");
		Hero2 h2 = new Hero2("ねこ");
		Hero2 h3 = new Hero2("ねこ7");

		/*
		 * Wand w = new Wand("杖"); System.out.println(w.getName()); Wizard2 wz =
		 * new Wizard2("魔女11"); SuperHero sh = new SuperHero("ヒーロー"); SuperHero
		 * sh2 = new SuperHero(""); Weapon we = new Weapon();
		 */
		// Item i = new Item("");
		PoisonMatango pm = new PoisonMatango('A');
		pm.setSuffix('B');
		/*
		 * System.out.println(pm.getSuffix() + "はPoisonMatango、pmの名前");
		 * w.setName("ねこ34"); sh.setName("スーパーヒーロー");
		 * System.out.println(sh.getName() + "SuperHeroの名前");
		 * System.out.println(we.getName() + "weaponの名前"); sh.run(); sh.fly();
		 * sh.sleep(); sh.slip();
		 */
		// h1.hp = 200;
		// h2.hp = 500;
		// wz.heal(h);
		h3.sleep();
		h3.sit(4);

		// King k = new King();
		// k.talk(h);

		// System.out.println(Hero2.name);
		System.out.println(h2.getName());
		System.out.println(h3.getName());
		h.setName("ねこ８");

		/*
		 * System.out.println(w.getName()); w.setName("Stick");
		 * System.out.println(w.getName()); System.out.println(w.getPower());
		 * w.setPower(3); System.out.println(w.getPower()); wz.setName("魔女2");
		 * System.out.println(wz.getName());
		 */
		// System.out.println(h1.hp);
		// Hero2.money = 400;
		// System.out.println(Hero2.money);
		// System.out.println(h1.money);
		// h2.money = 30;
		// System.out.println(h1.money);
		// System.out.println(Hero2.money);
		// System.out.println(h1.money + h2.money);
		System.out.println(Hero2.money);
		// Hero2.setRandomMoney();
		System.out.println(Hero2.money);
		// Hero2 h1 = new Hero2();
		System.out.println(h1.money);
		System.out.println(Hero2.money);

		// System.out.println(h1.hp);
		// System.out.println(h2.hp);

		Matango2 m1 = new Matango2();
		Matango2 m2 = new Matango2("ねこ３");
		m2.hp = 30;
		System.out.println(m1.name);
		System.out.println(m2.hp);
		System.out.println(m2.name);
		// ダンサー
		System.out.println(m1.getHp());
		Dancer d = new Dancer("ダンサー");
		d.attack(m1);
		System.out.println(d.getName());
		System.out.println(m1.getHp());
		d.read(m1);
		System.out.println(m1.getHp());

		h2.setName("ねこ13");
		System.out.println(h2.getName());
		System.out.println(h2.getName() + "のヒーローポイントは" + h3.getHp() + "です");

		// m1.hp = 50;
		// m1.suffix = 'A';

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

		Book k = new Book("本", 980, "赤", "1234");
		System.out.println(k.getIsbn() + "番の本" + k.getName() + "は" + k.getPrice() + "円です");

	}

}
```

■Asset.java

```java
public abstract class Asset {
	private String name;
	private int price;

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

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

	public int getPrice() {
		return this.price;
	}

}
```

■TangibleAsset.java

```java

public abstract class TangibleAsset extends Asset implements Thing {
	private String color;
	private double weight;

	public TangibleAsset(String name, int price, String color) {
		super(name, price);
		this.color = color;
	}

	public String getColor() {
		return this.color;
	}

	public double getWeight() {
		return this.weight;

	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

}
```

■Thing.java

```java
public interface Thing {
	double getWeight();

	void setWeight(double weight);

}
```

■Book.java

```java
public class Book extends TangibleAsset {
	private String color;
	private String isbn;

	public Book(String name, int price, String color, String isbn) {
		super(name, price, color);
		this.isbn = isbn;
	}

	// public String getName() {
	// return this.name;
	// }
	//
	// public int getPrice() {
	// return this.price;
	// }
	//
	// public String getColor() {
	// return this.color;
	// }

	public String getIsbn() {
		return this.isbn;
	}

}
```

■Character.java

```java

public abstract class Character {
	String name;
	int hp;

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

	public void run() {
		System.out.println(this.name + "は逃げ出した");
	}

	public abstract void attack(Matango2 m1);
	/*
	 * System.out.println(this.name + "の攻撃!"); m.hp -= ??;
	 * System.out.println("敵に??ポイントのダメージを与えた!");
	 */

	public abstract void read(Matango2 m1);

}
```

■CleaningService.java

```java

public class CleaningService {
	Shirt washShirt(Shirt s);

	Towl washTowl(Towl t);

	Coat washCoat(Coat c);

}
```

■Computer.java

```java

public class Computer extends TangibleAsset {
	private String name;
	private int price;
	private String color;
	private String makerName;

	public Computer(String name, int price, String color, String makerName) {
		this.name = name;
		this.price = price;
		this.color = color;
		this.makerName = makerName;
	}

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

	public int getPrice() {
		return this.price;
	}

	public String getColor() {
		return this.color;
	}

	public String getMakerName() {
		return this.makerName;
	}

}
```

■Creature.java

```java

public interface Creature {
	public static final double PI = 3.14;

	void run();

}
```

■Dancer.java

```java

public class Dancer extends Character {

	private String name;

	Dancer(String name) {
		this.name = name;
	}

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

	public void dance() {
		System.out.println(this.name + "は情熱的に踊った");
	}

	public void attack(Matango2 m1) {
		System.out.println(this.name + "の攻撃");
		System.out.println("敵に3ポイントのダメージ");
		m1.hp -= 3;
	}

	public void read(Matango2 m1) {
		System.out.println(this.name + "は読んだ");
		m1.hp += 2;
	}

}
```
■Fool.java

```java

public class Fool extends Character implements Human {
	public void attack(Matango2 m2) {
		System.out.println(this.getName() + "は、戦わず遊んでいる。");

	}

	public void talk() {

	}

	public void watch() {

	}

	public void hear() {

	}

	public void run() {

	}
}
```

■Hero2.java

```java
public class Hero2 {

	private String name;
	private int hp;
	static int money;
	// Sword2 sword;
	static int money2;

	public int getHp() {
		return this.hp;
	}

	public void setHp(int hp) {
		this.hp = hp;
	}

	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);
	}

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

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

	void setRandomMoney() {
		Hero2.money = (int) (Math.random() * 1000);
		System.out.println(name + "たちの所持金を初期化しました");
	}

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

	public void attack(Matango2 m) {

	}

	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 + "ポイント回復した.");

	}

	private void die() {
		System.out.println(this.name + "は死んでしまった！");
		System.out.println("GAME OVERです。");
	}

	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 + "でした");

	}
}
```

■Human.java

```java

public interface Human extends Creature {
	void talk();

	void watch();

	void hear();

}
```
■KyotoCleaningShop.java

```java

public class KyotoCleaningShop implements CleaningService {
		private String ownerName;
		private String address;
		private String phone;

		public Shirt washShirt(Shirt s) {
			return s;
		}

		public Towl washTowl(Towl t) {

		}

		public Coat washCoat(Coat c) {

		}
	}

}
```
■Matango2.java

```java
public class Matango2 {
	int hp = 50;
	final int LEVEL = 10;
	private char suffix;
	int sleepPoint;
	String name;

	public Matango2(char suffix) {
		this.suffix = suffix;
	}

	Matango2(String name) {
		this.hp = 10;
		this.name = name;
	}

	public int getHp() {
    	return this.hp;
    }

	Matango2() {
		this("ダミー２");
	}

	void run() {
		System.out.println("お化けキノコ" + this.suffix + "は逃げ出した！");
	}

	public void setSuffix(char suffix) {
		this.suffix = suffix;
	}

	public char getSuffix() {
		return this.suffix;
	}

	void sleep(int sleepPoint) {
		hp += sleepPoint;
		System.out.println("お化けキノコ" + this.suffix + "は" + sleepPoint + "眠った!" + this.hp + "ポイント回復した！");
	}

	public void attack(Hero2 h) {
		System.out.println("キノコ" + this.suffix + "の攻撃");
		System.out.println("10のダメージ");
		h.setHp(h.getHp() - 10);

	}

}
```

■PoisonMatango.java

```java
public class PoisonMatango extends Matango2 {
	private int poisonCount = 5;

	public PoisonMatango(char suffix) {
		super(suffix);
	}

	public void attack(Hero2 h) {
		super.attack(h);

		if (this.poisonCount > 0) {
			System.out.println("更に毒の胞子をばらまいた！");
			int dmg = h.getHp() / 5;
			h.setHp(h.getHp() - dmg);
			System.out.println(dmg + "ポイントのダメージ");
			poisonCount--;
		}

	}

}
```
■PrincessHero.java

```java

public class PrincessHero {
	implements Hero, Princess, Character {

	}

}
```

■実行結果
ねこ7は、眠って回復した！
ねこ7は、4秒座った！
HPが4ポイント回復した.
ねこ
ねこ7
ねこ８
0
0
0
0
ダミー２
30
ねこ３
10
ダンサーの攻撃
敵に3ポイントのダメージ
ダンサー
7
ダンサーは読んだ
9
ねこ13
ねこ13
ねこ13のヒーローポイントは104です
1234番の本本は980円です