Java面向对象之构造方法模拟捕鱼达人游戏【附源码】

1、创建捕手类

public class Test02_Capture{
	//定义类的属性变量
	String name;  //名字
	int food;  //饵料数量
	int food_out;  //捕手丢出的饵料
	int cap_fish;  //已经抓到的鱼数量
	
	//定义类的构造函数
	Test02_Capture(String name, int food, int food_out, int cap_fish){
		this.name = name;
		this.food = food;
		this.food_out = food_out;
		this.cap_fish = cap_fish;
	}
	
	//定义捕手方法:捕鱼(传入对象鱼)
	public void hook(Test02_Fish tf){
		System.out.println( this.name + "在开始捕捉" + tf.name + "~");
		this.food -= food_out;  //饵料数量减少
		tf.fish_num -= this.food_out;  //鱼的数量减少
	}
}

2、创建鱼类

public class Test02_Fish{
	//定义类的属性变量
	String name;  //名字
	int fish_num;  //鱼数量
	int fish_out;  //鱼逃出渔网的数量
	
	//定义类的构造函数
	public Test02_Fish(String name, int fish_num, int fish_out){
		this.name = name;
		this.fish_num = fish_num;
		this.fish_out = fish_out;
	}
	
	//定义鱼的方法:逃出渔网(传入对象捕手)
	public void escape(Test02_Capture tc){
		System.out.println(this.fish_out + "只" + this.name + "正在逃出" + tc.name+ "的渔网~");
		this.fish_num += fish_out;  //鱼的数量增加
		tc.cap_fish += tc.food_out-fish_out;  //捕手已经抓到的鱼数量=丢出的饵料-逃出的鱼
		System.out.println("已经抓到的鱼有:" + tc.cap_fish + "只。");
		System.out.println("鱼的数量还剩:"+ this.fish_num + "只。");
	}
}

3、创建用户类

public class Test02_Client{
	public static void main(String[] args){
		//第一轮捕鱼
		//创建捕手、鱼类的对象
		//初始一号捕手,饵料数量100,丢出饵料5, 已经捕到的鱼0
		Test02_Capture TC = new Test02_Capture("一号捕手", 100, 5, 0);
		//初始金枪鱼, 池塘鱼的数量500, 逃出渔网的鱼4
		Test02_Fish TF = new Test02_Fish("金枪鱼", 500, 4);
		
		
		System.out.println("------------------第一轮捕鱼--------------------");
		TC.hook(TF);  //开始捕鱼,传入鱼类(鱼的数量)
		TF.escape(TC);  //鱼逃渔网,传入捕手类(已经捕捉到的)
		
		//第二轮捕鱼
		//初始二号捕手,饵料数量100,丢出饵料30, 已经捕到的鱼1
		Test02_Capture TC2 = new Test02_Capture("二号捕手", 95, 30, 1);
		//初始红鲤鱼, 池塘鱼的数量499, 逃出渔网的鱼4
		Test02_Fish TF2 = new Test02_Fish("金枪鱼", 499, 2);
		
		System.out.println("------------------第二轮捕鱼--------------------");
		TC2.hook(TF2);  //开始捕鱼,传入鱼类(鱼的数量)
		TF2.escape(TC2);  //鱼逃渔网,传入捕手类(已经捕捉到的)
	}
}

4、测试

4.1
4.2

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸿蒙Next

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值