第9章
コード09-01
Hero.java
public class Hero {
String name;
int hp;
public void sleep() {
this.hp = 100;
System.out.println(this.name + "は、眠って回復した!");
}
public void sit(int sec) {
this.hp += sec;
System.out.println(this.name + "は、" + sec + "秒座った!");
System.out.println("HPが" + sec + "ポイント回復した");
}
public void slip() {
this.hp -= 5;
System.out.println(this.name + "は、転んだ!");
System.out.println("5のダメージ!");
}
public void run() {
System.out.println(this.name + "は、逃げ出した!");
System.out.println("GAMEOVER");
System.out.println("最終HPは" + this.hp + "でした");
}
}
Main.java
public class Main{
public static void main(String[] args) {
Hero h;
h = new Hero();
h.hp = 100;
}
}
コード09-02
Hero.java
public class Hero {
String name;
int hp;
public void sleep() {
this.hp = 100;
System.out.println(this.name + "は、眠って回復した!");
}
public void sit(int sec) {
this.hp += sec;
System.out.println(this.name + "は、" + sec + "秒座った!");
System.out.println("HPが" + sec + "ポイント回復した");
}
public void slip() {
this.hp -= 5;
System.out.println(this.name + "は、転んだ!");
System.out.println("5のダメージ!");
}
public void run() {
System.out.println(this.name + "は、逃げ出した!");
System.out.println("GAMEOVER");
System.out.println("最終HPは" + this.hp + "でした");
}
}
Main.java
public class Main {
public static void main(String[] args) {
Hero h1;
h1 = new Hero();
h1.hp = 100;
Hero h2;
h2 = h1;
h2.hp = 200;
System.out.println(h1.hp);
}
}
コード09-03
Hero.java
// 次にHeroクラスを定義する
public class Hero {
String name;
int hp;
Sword sword;
public void attack() {
System.out.println(this.name + "は攻撃した!");
System.out.println("敵に5ポイントのダメージをあたえた!");
}
}
Sword.java
// まず、Swordクラスを定義しておく
public class Sword {
String name;
int damage;
}
コード09-04
Hero.java
// 次にHeroクラスを定義する
public class Hero {
String name;
int hp;
Sword sword;
public void attack() {
System.out.println(this.name + "は攻撃した!");
System.out.println("敵に5ポイントのダメージをあたえた!");
}
}
Main.java
public class Main {
public static void main(String[] args) {
Sword s = new Sword();
s.name = "炎の剣";
s.damage = 10;
Hero h = new Hero();
h.name = "ミナト";
h.hp = 100;
h.sword = s;
System.out.println("現在の武器は" + h.sword.name);
}
}
Sword.java
// まず、Swordクラスを定義しておく
public class Sword {
String name;
int damage;
}
コード09-05
Hero.java
public class Hero {
String name;
int hp;
Sword sword;
public void attack() {
System.out.println(this.name + "は攻撃した!");
System.out.println("敵に5ポイントのダメージをあたえた!");
}
}
Sword.java
public class Sword {
String name;
int damage;
}
Wizard.java
public class Wizard {
String name;
int hp;
public void heal(Hero h) {
h.hp += 10;
System.out.println(h.name + "のHPを10回復した!");
}
}
コード09-06
Hero.java
public class Hero {
String name;
int hp;
Sword sword;
public void attack() {
System.out.println(this.name + "は攻撃した!");
System.out.println("敵に5ポイントのダメージをあたえた!");
}
}
Main.java
public class Main {
public static void main(String[] args) {
Hero h1 = new Hero();
h1.name = "ミナト";
h1.hp = 100;
Hero h2 = new Hero();
h2.name = "アサカ";
h2.hp = 100;
Wizard w = new Wizard();
w.name = "スガワラ";
w.hp = 50;
w.heal(h1); // ミナトを回復させる(HP: 100 -> 110)
w.heal(h2); // アサカを回復させる(HP: 100 -> 110)
w.heal(h2); // アサカを回復させる(HP: 110 -> 120)
}
}
Sword.java
public class Sword {
String name;
int damage;
}
Wizard.java
public class Wizard {
String name;
int hp;
public void heal(Hero h) {
h.hp += 10;
System.out.println(h.name + "のHPを10回復した!");
}
}
コード09-07
Main.java
public class Main {
public static void main(String[] args) {
String s = new String("こんにちは");
System.out.println(s);
}
}
コード09-08
Hero.java
public class Hero {
String name;
int hp;
Sword sword;
public void attack() {
System.out.println(this.name + "は攻撃した!");
System.out.println("敵に5ポイントのダメージをあたえた!");
}
public Hero() {
this.hp = 100; // hpフィールドを100で初期化
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード09-09
Hero.java
public class Hero {
String name;
int hp;
Sword sword;
public void attack() {
System.out.println(this.name + "は攻撃した!");
System.out.println("敵に5ポイントのダメージをあたえた!");
}
public Hero() {
this.hp = 100; // hpフィールドを100で初期化
}
}
Main.java
public class Main {
public static void main(String[] args) {
Hero h = new Hero();
System.out.println(h.hp);
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード09-10
Hero.java
public class Hero {
String name;
int hp;
Sword sword;
public void attack() {
System.out.println(this.name + "は攻撃した!");
System.out.println("敵に5ポイントのダメージをあたえた!");
}
public Hero(String name) {
this.hp = 100;
this.name = name; // 引数の値でnameフィールドを初期化
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード09-11
Hero.java
public class Hero {
String name;
int hp;
Sword sword;
public void attack() {
System.out.println(this.name + "は攻撃した!");
System.out.println("敵に5ポイントのダメージをあたえた!");
}
public Hero(String name) {
this.hp = 100;
this.name = name; // 引数の値でnameフィールドを初期化
}
}
Main.java
public class Main {
public static void main(String[] args) {
Hero h = new Hero("ミナト");
System.out.println(h.hp);
System.out.println(h.name);
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード09-12
Hero.java
public class Hero {
String name;
int hp;
Sword sword;
public void attack() {
System.out.println(this.name + "は攻撃した!");
System.out.println("敵に5ポイントのダメージをあたえた!");
}
public Hero(String name) {
this.hp = 100;
this.name = name; // 引数の値でnameフィールドを初期化
}
public Hero() {
this.hp = 100;
this.name = "ダミー";
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード09-13
Hero.java
public class Hero {
String name;
int hp;
Sword sword;
public void attack() {
System.out.println(this.name + "は攻撃した!");
System.out.println("敵に5ポイントのダメージをあたえた!");
}
public Hero(String name) {
this.hp = 100;
this.name = name; // 引数の値でnameフィールドを初期化
}
public Hero() {
this.hp = 100;
this.name = "ダミー";
}
}
Main.java
public class Main {
public static void main(String[] args) {
Hero h1 = new Hero("ミナト");
System.out.println(h1.name);
Hero h2 = new Hero();
System.out.println(h2.name);
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード09-14
Map.java
public class Map {
/* : */
public Map() {
}
}
コード09-15
Hero.java
public class Hero {
String name;
int hp;
Sword sword;
public void attack() {
System.out.println(this.name + "は攻撃した!");
System.out.println("敵に5ポイントのダメージをあたえた!");
}
public Hero(String name) { // コンストラクタ①
this.hp = 100;
this.name = name;
}
public Hero() { // コンストラクタ②
this.Hero("ダミー");
}
}
Sword.java
public class Sword {
String name;
int damage;
}
コード09-16
Hero.java
public class Hero {
String name;
int hp;
Sword sword;
public void attack() {
System.out.println(this.name + "は攻撃した!");
System.out.println("敵に5ポイントのダメージをあたえた!");
}
public Hero(String name) { // コンストラクタ①
this.hp = 100;
this.name = name;
}
public Hero() { // コンストラクタ②
this("ダミー");
}
}
Sword.java
public class Sword {
String name;
int damage;
}