(封装、继承、多态、接口)游戏角色练习

本文详细介绍了在面向对象编程中,如何设计角色类及其继承结构,包括法师和士兵等角色,每种角色都有其特定的攻击方式。此外,还展示了如何创建团队类来计算所有成员的总攻击力,以及如何通过接口实现武器和魔法棒的使用,增加了角色的攻击能力。
摘要由CSDN通过智能技术生成

描述如下:

class Magicer extends Role{

	private int magicLv;
	private int magicBasic=5;
	
	public Magicer(){}
	
	public Magicer(String name,int lv){
		super(name);
		if(lv<1 || lv>10){
			System.out.println("输入的值有误");
			return;
		}else{
			this.magicLv=lv;
		}
	} 
	
	
	public int attack(){
		return magicLv*magicBasic;
	}
	
	
	public int getMagicLv(){
		return this.magicLv;
	}
	
	public void setMagicLv(int lv){
		if(lv<1 || lv>10){
			System.out.println("输入的值有误");
			return;
		}else{
			this.magicLv=lv;
		}
	}

	public int getMagicBasic() {
		return magicBasic;
	}

	public void setMagicBasic(int magicBasic) {
		this.magicBasic = magicBasic;
	}
	
	
}
class Soldier extends Role{
	
	private int damage;
	
	public Soldier(){}
	
	public Soldier(String name,int damage){
		super(name);
		this.damage=damage;
	}
	
	
	public int attack() {
		return damage;
	}

	
	public int getDamage() {
		return damage;
	}

	public void setDamage(int damage) {
		this.damage = damage;
	}	
}
abstract class Role {

	private String name;
	
	public Role(){}
	
	public Role(String name){
		this.name=name;
	}
	
	
	public abstract int attack();

	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
class Team {
	Role[] teamA=new Role[6];
	int counter=0;
	
	public Team(){}

	public void addMember(Role role){
		if(counter==6){
			System.out.println("队伍满员");
			return;
		}
		
		teamA[counter]=role;
		counter++;
	}
	
	public int attackSum(){
		int sum=0;
		for(int i=0;i<counter;i++){
			sum+=teamA[i].attack();
		}
		return sum;
	}
}
class PlayGame {

	public static void main(String[] args) {

		Team team=new Team();
		Magicer m1=new Magicer("Mag11",1);
		Magicer m2=new Magicer("Mag22",2);
		Soldier s1=new Soldier("Sol11",11);
		Soldier s2=new Soldier("Sol22",22);
		team.addMember(m1);
		team.addMember(m2);
		team.addMember(s1);
		team.addMember(s2);

		System.out.println("总攻击为:"+team.attackSum());
	}

}

 

 

interface Weapon {
	//该方法表示设置武器的使用者
	void setSoldier(Soldier s);
	
	//该方法的返回值表示战士使用该武器时,对敌人的伤害值
	int fire();
}


interface MagicStick {

	int fire();
}


import java.util.Date;

class GreenStick implements MagicStick{

	@Override
	public int fire() {
		Date d=new Date();
		if(d.getMonth()>=6 &&d.getMonth()<=8){
			return 2;
		}
		return 1;
	}
}  


import java.util.Date;

class BlackStick implements MagicStick{

	@Override
	public int fire() {
		Date d=new Date();
		if(d.getMonth()%2==0){
			return 2;
		}
		return 1;
	}
}




abstract class NamedRole implements Role{

	private String name;
	private int age;
	
	public NamedRole(){}
	
	public NamedRole(String name){
		this.name=name;
	}
	
	public NamedRole(String name,int age){
		this.name=name;
		this.age=age;
	}

	
	public String getName() {
		return name;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
}


class Magicer extends NamedRole{

	private int magicLv=0;
	private int magicBasic=5;
	private MagicStick stick;
	
	public Magicer(){}
	
	public Magicer(String name,int age,int magicLv){
		super(name,age);
		if(magicLv<1 || magicLv>10){
			System.out.println("输入的值有误");
			return;
		}else{
			this.magicLv=magicLv;
		}
	} 
	
	
	public int attack(){
		return magicLv*magicBasic;
	}
	
	public void practise(){
		if(stick!=null){
			this.magicLv=magicLv+1+stick.fire();
		}
		magicLv++;
	}
	
	
	public int getMagicLv(){
		return this.magicLv;
	}
	
	public void setMagicLv(int lv){
		if(lv<1 || lv>10){
			System.out.println("输入的值有误");
			return;
		}else{
			this.magicLv=lv;
		}
	}

	
	public MagicStick getStick() {
		return stick;
	}

	
	public void setStick(MagicStick stick) {
		this.stick = stick;
	}
}


interface Weapon {
	//该方法表示设置武器的使用者
	void setSoldier(Soldier s);
	
	//该方法的返回值表示战士使用该武器时,对敌人的伤害值
	int fire();
}


class Soldier extends NamedRole{
	
	private int damage;
	private Weapon weapon;
	String name;
	
	public Soldier(){}
	
	public Soldier(String name,int age,int damage){
		super(name,age);
		this.name=name;
		this.damage=damage;
	}
	
	
	
	public int attack() {
		if(weapon==null){
			return damage;
		}
		return damage+weapon.fire();
	}
	
	public void practise(){
		this.damage=damage+10;
	}

	
	
	public int getDamage() {
		return damage;
	}

	public void setDamage(int damage) {
		this.damage = damage;
	}

	
	public Weapon getWeapon() {
		return weapon;
	}

	
	public void setWeapon(Weapon weapon) {
		this.weapon = weapon;
		if(this.weapon instanceof Bolo){
			System.out.println(name+"装上了大刀");
		}
		if(this.weapon instanceof Pike){
			System.out.println(name+"装上了长矛");
		}
	}	
}

class Bolo implements Weapon{
	private Soldier s;

	@Override
	public void setSoldier(Soldier s) {
		this.s=s;
	}

	@Override
	public int fire() {
		if(s.getAge()>=18){
			return 100;
		}
		return 50;
	}
}


class Pike implements Weapon{

	private String pikeName;
	private Soldier s;
	
	public Pike(String pikeName){
		this.pikeName=pikeName;
	}

	@Override
	public void setSoldier(Soldier s) {

		this.s=s;
	}

	@Override
	public int fire() {
		if(s.getName().equals(pikeName)){
			return 1000;
		}
		return 25;
	}
}

class Team {
	Role[] teamA=new Role[6];
	int counter=0;
	
	public Team(){}

	public void addMember(Role role){
		if(counter==6){
			System.out.println("队伍满员");
			return;
		}
		
		teamA[counter]=role;
		counter++;
	}
	
	public int attackSum(){
		int sum=0;
		for(int i=0;i<counter;i++){
			sum+=teamA[i].attack();
		}
		return sum;
	}
}


public class PlayGame {

	public static void main(String[] args) {

		Team team=new Team();
		MagicStick m1s=new GreenStick();
		MagicStick m2s=new BlackStick();
		Magicer m1=new Magicer("Mag11",1,1);
		Magicer m2=new Magicer("Mag22",2,2);
		
		Weapon s1s=new Bolo();
		Weapon s2s=new Pike("Sol22");
		Soldier s1=new Soldier("Sol11",11,100);
		Soldier s2=new Soldier("Sol22",22,100);
		
		System.out.println("---法师组 空手练习---");
		System.out.println(m1.attack()+"\t"+m2.attack());
		m1.practise();
		m2.practise();
		System.out.println(m1.attack()+"\t"+m2.attack());
		
		System.out.println("---法师组 武器练习---");
		m1.setStick(m1s);
		m2.setStick(m2s);
		System.out.println(m1.attack()+"\t"+m2.attack());
		m1.practise();
		m2.practise();
		System.out.println(m1.attack()+"\t"+m2.attack());
		
		System.out.println("---战士组 空手练习---");
		System.out.println(s1.attack()+"\t"+s2.attack());
		s1.practise();
		s2.practise();
		System.out.println(s1.attack()+"\t"+s2.attack());
		
		System.out.println("---战士组 装武器练习---");
		s1.setWeapon(s1s);
		s1s.setSoldier(s1);
		s2.setWeapon(s2s);
		s2s.setSoldier(s2);
		System.out.println(s1.attack()+"\t"+s2.attack());
		s1.practise();
		s2.practise();
		System.out.println(s1.attack()+"\t"+s2.attack());
		
		team.addMember(m1);
		team.addMember(m2);
		team.addMember(s1);
		team.addMember(s2);

		System.out.println("总攻击为:"+team.attackSum());
	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值