5—19java作业

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值