Created
July 24, 2010 01:47
-
-
Save johobemax/488303 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 勇者クラス | |
*/ | |
class Hero | |
{ | |
String name; | |
int hp; | |
int lv; | |
/** | |
* 勇者を生成する | |
*/ | |
Hero() | |
{ | |
name = "太朗"; | |
hp = 100; | |
lv = 1; | |
} | |
/** | |
* 勇者の名前を指定して、勇者を生成する | |
* @param name 勇者の名前 | |
*/ | |
Hero(String name) | |
{ | |
this.name = name; | |
hp = 100; | |
lv = 1; | |
} | |
/** | |
* 勇者の名前、体力、レベルを指定して、勇者を生成する | |
* @param name 勇者の名前 | |
* @param hp 勇者の体力 | |
* @param lv 勇者のレベル | |
*/ | |
Hero(String name, int hp, int lv) | |
{ | |
this.name = name; | |
this.hp = hp; | |
this.lv = lv; | |
} | |
/** | |
* 勇者のステータスを表示する | |
*/ | |
void status() | |
{ | |
System.out.println("name=>" + name); | |
System.out.println("hp=>" + hp); | |
System.out.println("lv=>" + lv); | |
} | |
} | |
/** | |
* 戦隊クラス | |
*/ | |
class Party | |
{ | |
Hero[] party; | |
/** | |
* 戦隊を生成する | |
*/ | |
public Party() | |
{ | |
party = new Hero[3]; | |
party[0] = new Hero("太朗",100,1); | |
party[1] = new Hero("次郎",150,5); | |
party[2] = new Hero("三郎",200,10); | |
} | |
/** | |
* 戦隊のメンバーを表示する | |
*/ | |
void member() | |
{ | |
System.out.println("先頭=>" + party[0].name); | |
System.out.println("真ん中=>" + party[1].name); | |
System.out.println("最後尾=>" + party[2].name); | |
} | |
/** | |
* 戦隊メンバーのステータスを表示する | |
*/ | |
void statusAll() | |
{ | |
for(Hero h:party){ | |
h.status(); | |
} | |
} | |
} | |
/** | |
* 勇者クラスをテストする | |
*/ | |
public class HeroTest | |
{ | |
public static void main(String[] args) | |
{ | |
Party p = new Party(); //戦隊を生成 | |
p.member(); //メンバーを表示する | |
p.statusAll(); //メンバーのステータスを表示する | |
} | |
} |
実行結果
先頭=>太朗
真ん中=>次郎
最後尾=>三郎
name=>太朗
hp=>100
lv=>1
name=>次郎
hp=>150
lv=>5
name=>三郎
hp=>200
lv=>10
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
コンストラクタの勉強用サンプルソース