/*
*本bolog为吉软58班作业贴 更新日期:2018年6月4日 最后更新时间:12:45
*使用本博代码请与博主联系
*/
第九次作业题目如下:
父类抽象类Role:
package edu.ccst.jlu;public abstract class Role {private String name;Role(String name){this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int attack(){return 0;}}
子类Magicer:
package edu.ccst.jlu;public class Magicer extends Role{Magicer(String name) {super(name);// TODO Auto-generated constructor stub}//跟随父类的构造private int MagLevel;public int getMagLevel() {return MagLevel;}public void setMagLevel(int magLevel) {//魔法等级(范围为1 ~ 10)if(magLevel > 0 && magLevel <= 10){MagLevel = magLevel;}else{System.out.println("输入不合法");}}public int attack(){//该方法返回法师的攻击对敌人造成的伤害值。 //法师攻击伤害值为:魔法等级*魔法基本伤害值(固定为5)return MagLevel*5;}}
子类Soldier:
package edu.ccst.jlu;public class Soldier extends Role{Soldier(String name) {super(name);// TODO Auto-generated constructor stub}//跟随父类的构造private int AttackNum;public int getAttackNum() {return AttackNum;}public void setAttackNum(int attackNum) {AttackNum = attackNum;}public int attack(){//该方法返回战士的攻击对敌人造成的伤害值。 战士的攻击伤害值为:其攻击伤害属性值return AttackNum;}
}
Team类:
package edu.ccst.jlu;public class Team {//用于记录现在有几个队员private int counter;//声明队伍数组private Role[] roles;//一堆getsetpublic int getCounter() {return counter;}public void setCounter(int counter) {this.counter = counter;}public Role[] getRoles() {return roles;}public void setRoles(Role[] roles) {this.roles = roles;}//构造函数public Team(){ //没有给team类设置私有名字,以免使用时发生歧义counter = 0; //初始0人roles = new Role[6];//队员上限6人}public void addMember(Team t,Role r){//表示向队伍t内,新增队员rt.roles[counter] = r;counter ++; }public int attackSum(){//表示组队所有成员进行攻击时,对敌人造成的总伤害值int totalAttack = 0;//总攻击力初值为0for(int i = 0;i < counter;i ++){totalAttack = totalAttack +roles[i].attack();}//攻击力累加return totalAttack;}
}
测试类:
package edu.ccst.jlu;public class Test {public static void main(String[] args) {//创建角色Magicer r1 = new Magicer("法师");Soldier r2 = new Soldier("战士");//创建小队Team t = new Team();t.addMember(t, r1);t.addMember(t, r2);//取出队伍数组Role[] r = t.getRoles();//测试构造System.out.println("现在我们的小队有"+t.getCounter()+"个人");System.out.print("他们是:");for(int i = 0;i < t.getCounter();i ++){System.out.print(r[i].getName() + " ");}System.out.println("");//测试攻击System.out.println("他们目前的攻击力分别是:");for(int i = 0;i < t.getCounter();i ++){System.out.println(r[i].getName() + ":" + r[i].attack());}//给法师等级,给战士攻击力后再次测试r1.setMagLevel(9);r2.setAttackNum(30);System.out.println("现在他们目前的攻击力分别是:");for(int i = 0;i < t.getCounter();i ++){System.out.println(r[i].getName() + ":" + r[i].attack());}}
}
运行结果输出显示: