Java理解抽象类与接口游戏人物简单案例

为了更好地理解和掌握抽象类与接口的概念,做了一个以简单游戏人物设计来加深理解的案例。

1、首先分析游戏人物的类型假如分为战士、法师和射手类英雄他们三种同属于英雄有姓名,购买价格,英雄生命值属性,都有行走、释放技能的行为而每种英雄的技能有可能不一样所以将技能的方法设置为抽象方法。

package com.miaoxiake.day03;
/**
 * 英雄类
 * @author 喵喵侠客
 *
 */
public abstract class Hero {
	String name;
	int price;
	int life;
	public Hero() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Hero(String name, int price, int life) {
		super();
		this.name = name;
		this.price = price;
		this.life = life;
	}
	
	public abstract void skill();
	
	public void run() {
		System.out.println("走路。。。");
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public int getLife() {
		return life;
	}
	public void setLife(int life) {
		this.life = life;
	}
	
	
}

2、定义一个可以让英雄瞬间移动的接口,如果某类英雄想拥有瞬间移动的技能直接实现接口即可。

package com.miaoxiake.day03;
/**
 * 瞬间移动,闪现功能
 * @author 喵喵侠客
 *
 */
public interface FlashesAbility {
	void setAction();
}

3.创建战士、法师、射手类继承英雄抽象类

package com.miaoxiake.day03;
/**
 * 战士类
 * @author 喵喵侠客
 *
 */
public class Warrior extends Hero {

	
	public Warrior() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Warrior(String name, int price, int life) {
		super(name, price, life);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void skill() {
		// TODO Auto-generated method stub
		System.out.println("物理伤害");
	}

}
package com.miaoxiake.day03;
/**
 * 法师类
 * @author 喵喵侠客
 *
 */
public class Master extends Hero {

	
	public Master() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Master(String name, int price, int life) {
		super(name, price, life);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void skill() {
		// TODO Auto-generated method stub
		System.out.println("法术攻击");
	}

}

让射手类实现瞬间移动接口,使射手拥有此功能

package com.miaoxiake.day03;
/**
 * 射手类
 * @author 喵喵侠客
 *
 */
public class Shooter extends Hero implements FlashesAbility{

	
	public Shooter() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Shooter(String name, int price, int life) {
		super(name, price, life);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void skill() {
		// TODO Auto-generated method stub
		System.out.println("远程射击");
	}

	@Override
	public void setAction() {
		// TODO Auto-generated method stub
		System.out.println("可以瞬间移动");
	}

}

4、创建测试类

package com.miaoxiake.test;

import com.miaoxiake.day03.Hero;
import com.miaoxiake.day03.Master;
import com.miaoxiake.day03.Shooter;
import com.miaoxiake.day03.Warrior;

public class HeroTest {
	public static void main(String[] args) {
		Hero h1=new Warrior("亚瑟",888,1000);//定义一个战士类对象
		Hero h2=new Shooter("马可波罗",666,600);//定义一个射手类对象
		Hero h3=new Master("王昭君",888,500);//定义一个法师类对象
		System.out.println("英雄名称:"+h1.getName()+";  购买价格:"+h1.getPrice()+
				"金币;   生命值:"+h1.getLife());
		System.out.print("基本技能:");
		h1.run();
		System.out.print("技能:");
		h1.skill();
		System.out.println("*********************");
		System.out.println("英雄名称:"+h2.getName()+";  购买价格:"+h2.getPrice()+
				"金币;   生命值:"+h2.getLife());
		System.out.print("基本技能:");
		h1.run();
		System.out.print("技能:");
		h2.skill();
		System.out.print("增加功能:");
		((Shooter) h2).setAction();
		System.out.println("*********************");
		System.out.println("英雄名称:"+h3.getName()+";  购买价格:"+h3.getPrice()+
				"金币;   生命值:"+h3.getLife());
		System.out.print("基本技能:");
		h1.run();
		System.out.print("技能:");
		h3.skill();
		
		
	}
}

最后运行查看效果

 好了,到此结束。希望对大家能有所帮助

猜你喜欢

转载自blog.csdn.net/qq_38217237/article/details/81101714
今日推荐