java基础之面向对象的运用

奥特曼打怪兽

创建AoTeMan类

package com.qf.day8;

public class AoTeMan {
//	奥特曼打怪兽,奥特曼有名字、攻击力、血量等属性,有技能:飞行、光线攻击;
	String name;//名字
	int str;//攻击力
	int hp;//血量
	
	public AoTeMan() {
		super();
		// TODO Auto-generated constructor stub
	}

	public AoTeMan(String name, int str, int hp) {
		super();
		this.name = name;
		this.str = str;
		this.hp = hp;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getStr() {
		return str;
	}

	public void setStr(int str) {
		this.str = str;
	}

	public int getHp() {
		return hp;
	}

	public void setHp(int hp) {
		this.hp = hp;
	}

	public void feiXing(AoTeMan m) {
		m.setHp(m.getHp()+80);
		System.out.println(m.getName()+"被动技能飞行激活了,"+m.getName()+"恢复了"+100+"血量"+ ",剩余血量" + m.getHp());
	}
	public void guangXGJ(AoTeMan m,GuaiShou n) {
		n.setHp(n.getHp() - m.getStr());
		System.out.println(n.getName() + "被" + m.getName() +"使用光线技能"+ "攻击了,");
		System.out.println(n.getName() + "掉血" + m.getStr() + ",剩余血量" + n.getHp());
	}
}

创建GuaiShou类

package com.qf.day8;

public class GuaiShou {
//	怪兽有名字、攻击力和血量属性,有技能:奔跑、蛮力攻击。
	String name;//名字
	int str;//攻击力
	int hp;//血量
	
	public GuaiShou() {
		super();
		// TODO Auto-generated constructor stub
	}
	public GuaiShou(String name, int str, int hp) {
		super();
		this.name = name;
		this.str = str;
		this.hp = hp;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getStr() {
		return str;
	}
	public void setStr(int str) {
		this.str = str;
	}
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		this.hp = hp;
	}
	public void benPao(GuaiShou n) {
		n.setHp(n.getHp()+50);
		System.out.println(n.getName()+"被动技能奔跑激活了,"+n.getName()+"恢复了"+100+"血量"+ ",剩余血量" + n.getHp());
	}
	public void manLi(GuaiShou n,AoTeMan m) {
		m.setHp(m.getHp() - n.getStr());
		System.out.println(m.getName() + "被" + n.getName() +"使用蛮力技能"+ "攻击了,");
		System.out.println(m.getName() + "掉血" + n.getStr() + ",剩余血量" + m.getHp());
	}
}

创建Test_DaGuaiShou测试类

package com.qf.day8;

public class Test_DaGuaiShou {
	public static void main(String[] args) {
		AoTeMan aoTeMan = new AoTeMan("奥特曼", 80, 500);//创建奥特曼对象
		GuaiShou guaiShou = new GuaiShou("小怪兽", 30, 300);//创建小怪兽对象
		//奥特曼打怪兽
		int count = 1;
		while(aoTeMan.getHp()>0&&guaiShou.getHp()>0){
			System.out.println("\t"+"第"+count+"回合");
			aoTeMan.guangXGJ(aoTeMan,guaiShou);
			if(count %2 == 0){
				guaiShou.benPao(guaiShou);
			}
			if(guaiShou.getHp()<=0){
					System.out.println("小怪兽被打死了!"+"\n"+"gameover");
				break;
			}
			guaiShou.manLi(guaiShou,aoTeMan);
			if(count %2 != 0){
				aoTeMan.feiXing(aoTeMan);;
			}
			if(aoTeMan.getHp()<=0){
				System.out.println("奥特曼离我们而去了!"+"\n"+"gameover");
				break;
			}
			count++;
		}

	}

}

简答题

//	简答题:
//	1、面向对象和面向过程的区别
	//面向过程:解决问题时,考虑的是问题解决过程中的步骤。面向对象:解决问题时,首先考虑到问题过程中会有哪些参与者(对象),然后再考虑这些对象之间的关系。
//	2、什么是类,什么是对象
	//类是指具有相同行为(方法)和特征(属性)的同一类型的对象的一个抽象。例如:召唤师,英雄对象是指某一个类的一个具体化的事物。例如:亚索
//	3、如何定义类,如何定义对象
	//定义类:class 类名 {} 定义对象:类名 对象名  = new 类名();
//	4、构造方法有什么作用?如何定义:
	//作用:简化属性的设置过程;可以要求创建对象时就设置关键属性的值,即创建对象过程的可控性。 
	//如何定义:无参构造法public 类名( ){}  有参构造法public 类名(数据类型  变量名 ){}
//	5、this关键字的作用
	//this在Java语言中非常容易理解。指代当前对象。意思是谁调用就指代谁。通常用来与局部变量区分。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值