1030自主学習
This commit is contained in:
53
Question13-Extends/src/main/java/Character.java
Normal file
53
Question13-Extends/src/main/java/Character.java
Normal file
@@ -0,0 +1,53 @@
|
||||
|
||||
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 name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int attack() {
|
||||
return this.power;
|
||||
}
|
||||
|
||||
public boolean damage(int enemyAttack) {
|
||||
int damage = enemyAttack - this.defense; //ダメージ量の計算
|
||||
if(damage <= 0) {
|
||||
damage = 1; //計算結果が0以下ならダメージ量1にする
|
||||
}
|
||||
|
||||
this.hp -= damage; //hpからダメージ量引いて残りをhpにする
|
||||
boolean dead= false; //戦闘不能かどうかを真偽地を使って判断
|
||||
|
||||
if(this.hp <= 0) { //hpが0以下なら
|
||||
this.hp = 0; //hpを0に補正
|
||||
dead = true;
|
||||
}
|
||||
|
||||
System.out.println(this.name + "は" + damage + "のダメージを受けた");
|
||||
System.out.println(this.name + "の残りの体力は" + this.hp + "。");
|
||||
|
||||
if(dead) { //戦闘不能の時
|
||||
System.out.println(this.name + "はちからつきた。");
|
||||
}
|
||||
return dead;
|
||||
}
|
||||
|
||||
public String Parameters() { //パラメーターを文字列で返す
|
||||
return "名前:" + this.name + "\n" + "体力:" + this.hp + "\n" +
|
||||
"力" + this.power + "\n" + "耐久" + this.defense;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user