奥特曼打怪兽
创建AoTeMan类
package com.qf.day8;
public class AoTeMan {
String name;
int str;
int hp;
public AoTeMan() {
super();
}
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();
}
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语言中非常容易理解。指代当前对象。意思是谁调用就指代谁。通常用来与局部变量区分。