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);
|
||||
}
|
||||
}
|
||||
6
Question14-Interface/.classpath
Normal file
6
Question14-Interface/.classpath
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
|
||||
<classpathentry kind="src" path="src/main/java"/>
|
||||
<classpathentry kind="output" path="build/classes"/>
|
||||
</classpath>
|
||||
17
Question14-Interface/.project
Normal file
17
Question14-Interface/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Question14-Interface</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,9 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
|
||||
org.eclipse.jdt.core.compiler.compliance=21
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=21
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<faceted-project>
|
||||
<installed facet="java" version="21"/>
|
||||
</faceted-project>
|
||||
6
Question15-Polymorphism/.classpath
Normal file
6
Question15-Polymorphism/.classpath
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
|
||||
<classpathentry kind="src" path="src/main/java"/>
|
||||
<classpathentry kind="output" path="build/classes"/>
|
||||
</classpath>
|
||||
23
Question15-Polymorphism/.project
Normal file
23
Question15-Polymorphism/.project
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Question15-Polymorphism</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,9 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
|
||||
org.eclipse.jdt.core.compiler.compliance=21
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=21
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<faceted-project>
|
||||
<installed facet="java" version="21"/>
|
||||
</faceted-project>
|
||||
Reference in New Issue
Block a user