public class Hunter {
String nameString;
int life;
boolean isLive;
String weaponString;
public Hunter(String name,int life,boolean isLive,String weapon) {
// TODO Auto-generated constructor stub
this.nameString=name;
this.life=life;
this.isLive=isLive;
this.weaponString=weapon;
}
public void fight(Monster monster){
monster.injured();
}
public void injured(){
this.life=this.life-50;
if(this.life<=0){
this.dead();
}
}
public void dead(){
this.isLive=false;
System.out.println("英雄已经死了");
}
public void display(){
System.out.println("生命值"+this.life);
System.out.println("是否存活"+this.isLive);
System.out.println("使用武器"+this.weaponString);
System.out.println("英雄名称"+this.nameString);
}
}
public class Monster {
String type;
int life;
boolean isLive;
public Monster(String type,int life,boolean isLive) {
// TODO Auto-generated constructor stub
this.type=type;
this.life=life;
this.isLive=isLive;
}
public void injured(){
this.life=this.life-50;
if(this.life<=0){
this.dead();
}
}
public void kill(Hunter hunter) {
hunter.injured();
}
public void dead() {
this.isLive=false;
System.out.println("怪兽已经死了");
}
public void display() {
System.out.println("生命值"+this.life);
System.out.println("是否存活"+this.isLive);
System.out.println("怪兽名称"+this.type);
}
}
public class TestGame {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Hunter hunter1=new Hunter("张家辉", 100, true, "大砍刀");
Monster monster1=new Monster("僵尸王", 100, true);
hunter1.fight(monster1);
monster1.kill(hunter1);
hunter1.display();
monster1.display();
hunter1.fight(monster1);
monster1.kill(hunter1);
}
}
结果