5—19java作业

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 14:39   11   0
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);
  

 }

}

结果

转载于:https://my.oschina.net/u/3464871/blog/1815544

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP