10/30 14:05
This commit is contained in:
66
Question13-Extends/src/main/java/Character.java
Normal file
66
Question13-Extends/src/main/java/Character.java
Normal file
@@ -0,0 +1,66 @@
|
||||
public class Character {
|
||||
// 保持するデータ(完全非公開)
|
||||
private String name = "キャラクター";
|
||||
private int hp;
|
||||
private int power;
|
||||
private int defense;
|
||||
|
||||
// 生成方法:生成時に体力・力・防御力の初期値を指定できる
|
||||
public Character(int hp, int power, int defense) {
|
||||
this.hp = hp;
|
||||
this.power = power;
|
||||
this.defense = defense;
|
||||
}
|
||||
|
||||
// 名前のアクセサ(ゲッター / セッター)
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// 攻撃する:自身の「力」の値をそのまま返す
|
||||
public int attack() {
|
||||
return this.power;
|
||||
}
|
||||
|
||||
// ダメージを受ける(相手の攻撃値を受け取る)
|
||||
public boolean receiveDamage(int attackerPower) {
|
||||
// 1. ダメージ量 = 受け取った攻撃値 − 自身の防御力(0以下の場合は1とする)
|
||||
int damage = attackerPower - this.defense;
|
||||
if (damage <= 0) {
|
||||
damage = 1;
|
||||
}
|
||||
|
||||
// 2. 自身の体力からダメージ量を引く
|
||||
this.hp -= damage;
|
||||
|
||||
// 3. 体力が0以下になった場合は、体力を0に補正し、「戦闘不能」の状態とする
|
||||
boolean isDown = false;
|
||||
if (this.hp <= 0) {
|
||||
this.hp = 0;
|
||||
isDown = true;
|
||||
}
|
||||
|
||||
// 4. 次の内容をこの順番で出力する
|
||||
System.out.println(this.name + "は" + damage + "のダメージを受けた。");
|
||||
System.out.println(this.name + "の残り体力は" + this.hp + "。");
|
||||
if (isDown) {
|
||||
System.out.println(this.name + "はちからつきた。");
|
||||
}
|
||||
System.out.println("↓");
|
||||
|
||||
// 5. 戦闘不能になったかどうかを真偽値で返す
|
||||
return isDown;
|
||||
}
|
||||
|
||||
// パラメーター表示:指定の形式の複数行の文字列を返す
|
||||
public String showStatus() {
|
||||
return "名 前:" + this.name + "\n" +
|
||||
"体 力:" + this.hp + "\n" +
|
||||
" 力 :" + this.power + "\n" +
|
||||
"耐 久:" + this.defense;
|
||||
}
|
||||
}
|
||||
5
Question13-Extends/src/main/java/Hero.java
Normal file
5
Question13-Extends/src/main/java/Hero.java
Normal file
@@ -0,0 +1,5 @@
|
||||
class Hero extends Character {
|
||||
public Hero() {
|
||||
super(25, 10, 7);
|
||||
}
|
||||
}
|
||||
11
Question13-Extends/src/main/java/Q13_3.java
Normal file
11
Question13-Extends/src/main/java/Q13_3.java
Normal file
@@ -0,0 +1,11 @@
|
||||
public class Q13_3 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Hero hero = new Hero();
|
||||
|
||||
hero.setName("勇者");
|
||||
|
||||
String status = hero.showStatus();
|
||||
System.out.println(status);
|
||||
}
|
||||
}
|
||||
13
Question13-Extends/src/main/java/Q13_4.java
Normal file
13
Question13-Extends/src/main/java/Q13_4.java
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
public class Q13_4 {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Slime slime = new Slime();
|
||||
|
||||
slime.setName("スライム");
|
||||
|
||||
String status = slime.showStatus();
|
||||
System.out.println(status);
|
||||
}
|
||||
|
||||
}
|
||||
5
Question13-Extends/src/main/java/Slime.java
Normal file
5
Question13-Extends/src/main/java/Slime.java
Normal file
@@ -0,0 +1,5 @@
|
||||
class Slime extends Character {
|
||||
public Slime() {
|
||||
super(10, 5, 2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user