继承接口小练习
今天布置了一道继承接口练习题 欢迎大家订正
父类
/**
* 父类:英雄
*/
public abstract class Hero implements Action {
private String name;//英雄名字
public Hero() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Hero(String name) {
this.name = name;
}
}
接口类
/**
* 行动
*/
public interface Action {
void uchino();//打怪
}
/**
* 买武器
*/
public interface BuyEquipment {
void buy(Equipment e);
}
实体类
/**
* 刺客类 打野
*/
public class Assassin extends Hero implements BuyEquipment {
private int money=500;//金币
private int attackPower=100;//攻击力
private int spellPower=20;//法术强度
private boolean buywildKnife;//是否购买打野刀
private int level=1;
private int count=0;//击杀野怪数
public Assassin(String name) {
super(name);
}
public Assassin() {
super();
}
//打野方法
@Override
public void uchino() {
if (level<=16){
level++;
}else {
System.out.println("经验已满,已达最大等级");
}
if (buywildKnife){
attackPower+=2;
money+=1000;
System.out.println("已击杀,当前金币:"+money+" 当前等级:"+level+" 当前攻击力:"+attackPower);
}else{
money+=500;
System.out.println("已击杀,当前金币:"+money+" 当前等级:"+level+" 当前攻击力:"+attackPower);
}
count++;
System.out.println("已打"+count+"只野怪");
}
//购买装备
@Override
public void buy(Equipment e) {
if (money>=e.getMoney()){
money-=e.getMoney();
attackPower+=e.getAttackPower();
spellPower+=e.getSpellPower();
System.out.println("已购买"+e.getName()+",当前剩余金币为"+money+" 攻击力为:"+attackPower+" 法术强度为"+spellPower);
if (e.getName().equals("打野刀")){//是否购买打野刀
buywildKnife=true;
}
}else{
System.out.println("金币不足,无法购买");
}
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public int getAttackPower() {
return attackPower;
}
public void setAttackPower(int attackPower) {
this.attackPower = attackPower;
}
public int getSpellPower() {
return spellPower;
}
public void setSpellPower(int spellPower) {
this.spellPower = spellPower;
}
public boolean isBuywildKnife() {
return buywildKnife;
}
public void setBuywildKnife(boolean buywildKnife) {
this.buywildKnife = buywildKnife;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
if (level < 1 || level >= 16) {
System.out.println("level error!");
return;
}
this.level = level;
}
}
/**
* 装备类
*/
public class Equipment {
private String name;//武器名字
private int money;//花费金币
private int attackPower;//攻击力
private int spellPower;//法术强度
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public int getAttackPower() {
return attackPower;
}
public void setAttackPower(int attackPower) {
this.attackPower = attackPower;
}
public int getSpellPower() {
return spellPower;
}
public void setSpellPower(int spellPower) {
this.spellPower = spellPower;
}
public Equipment(String name, int money, int attackPower, int spellPower) {
this.name = name;
this.money = money;
this.attackPower = attackPower;
this.spellPower = spellPower;
}
public Equipment() {
}
}
测试类
public class Test {
public static void main(String[] args) {
boolean isOk = false;
Equipment e1=new Equipment();
e1.setName("打野刀");
e1.setMoney(300);
e1.setAttackPower(50);
e1.setSpellPower(0);
Equipment e2=new Equipment();
e2.setName("帽子");
e2.setMoney(500);
e2.setAttackPower(0);
e2.setSpellPower(100);
Equipment e3=new Equipment();
e3.setName("暴击剑");
e3.setMoney(500);
e3.setAttackPower(200);
e3.setSpellPower(0);
Scanner scan=new Scanner(System.in);
Assassin ass=new Assassin();
System.out.println("请输入英雄名:");
String name = scan.next();
ass.setName(name);
System.out.println("我是"+ass.getName()+"让我们开启打野之路");
do {
isOk=true;
System.out.println("输入1(打野),输入2(购买装备),输入0(退出):");
int action = scan.nextInt();
switch (action){
case 1:
ass.uchino();
break;
case 2:
System.out.println("输入购买装备1(打野刀),输入2(帽子),输入3(暴击剑):");
int e=scan.nextInt();
switch (e){
case 1:
ass.buy(e1);
break;
case 2:
ass.buy(e2);
break;
case 3:
ass.buy(e3);
break;
default:
System.out.println("没有此装备!");
}
break;
case 0:
isOk=false;
System.out.println("欢迎继续再来!");
break;
default:
System.out.println("error!");
}
}while (isOk);
}
}
写测试类时候遇到需要循环switch 忽然忘记怎么写 知道是do-while 但是忘了怎么用
补充一下:
do{
循环体
}while(条件)
do-while和while循环的区别 当括号里的条件一开始就成立时,while和do while的都循环,且次数是相同的。而当括号里的条件一开始就不成立时,do while会先执行一次,而while里面的循环体是不会执行的。