Role类
package org.jsoft.Game;
public abstract class Role {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//Soldier
public void setHurt(int hurt) {}
public int getHurt() {
return 0;
}
//Maficer
public int getLevel() {
return 0;
}
public void setLevel(int level) {}
public abstract int attack();
}
Magicer类
package org.jsoft.Game;
public class Magicer extends Role{
private int level;
public Magicer(){}
public Magicer(int level){
this.level=level;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
@Override
public int attack() {
return getLevel()*5;
}
}
Soldier类
package org.jsoft.Game;
public class Soldier extends Role{
private int hurt;
public Soldier(){}
public Soldier(int hurt){
this.hurt=hurt;
}
public int getHurt() {
return hurt;
}
public void setHurt(int hurt) {
this.hurt = hurt;
}
@Override
public int attack() {
// TODO Auto-generated method stub
return getHurt();
}
}
Team类
package org.jsoft.Game;
public class Team {
private Role [] team;
// public void setTeam(Role[] team) {
// this.team = team;
// }
public Role[] getTeam() {
return team;
}
public void setTeam(int i) {
if(i<7&&i>=0){
team=new Role[i];
}
else {
System.out.println("队伍人数不合格");
}
}
public int attackSum(){
int Total=0;
for(int i=0;i<getTeam().length;i++){
if(getTeam()[i]==null){
break;
}
Total+=getTeam()[i].attack();
}
return Total;
}
public void addMember(Role role){
for(int i=0;i<getTeam().length;i++){
if(getTeam()[i]==null){
getTeam()[i]=role;
break;
}
}
}
// public void addMember(Role role){
// try{
// Role[] r=new Role[6];
// r=getTeam();
// for(int i=0;i<6;i++){
// if(r[i]==null){
// r[i]=role;
// break;
// }
// }
// setTeam(r);
// }
// catch(Exception e){
// Role[] r=new Role[6];
// r[0]=role;
// setTeam(r);
// }
// }
}
//测试
package org.jsoft.Game;
import java.util.Scanner;
public class TestGame {
public static void main(String[] args) {
Role magicer1=new Magicer();
magicer1.setName("法师1");
magicer1.setLevel(10);
Role magicer2=new Magicer();
magicer2.setName("法师2");
magicer2.setLevel(6);
Role soldier1=new Soldier();
soldier1.setName("战士1");
soldier1.setHurt(18);
Role soldier2=new Soldier();
soldier2.setName("战士2");
soldier2.setHurt(16);
Team team=new Team();
team.setTeam(4);
team.addMember(magicer1);
team.addMember(magicer2);
team.addMember(soldier1);
team.addMember(soldier2);
System.out.println("队伍team造成的总伤害为:"+team.attackSum());
}
}