我正在制作一个带有物品库存的英雄的小游戏。
public enum Objects_type
{
WEAPON,
ARMOR
}
public abstract class Objects_class
{
protected String name;
protected Objects_type type;
public Objects_class(String name, Objects_type type)
{
this.name = name;
this.type = type;
}
}
public abstract class Armor extends Objects_class{
int life = 0;
int res_fire = 0;
public Armor(String name, int largeur, int hauteur) {
super(name, Objects_type.ARMOR);
}
}
public abstract class Weapon extends Objects_class
{
protected int dmg_fire = 0;
public Weapon(String name) {
super(name, Objects_type.WEAPON);
}
}
public class StickOfJoy extends Weapon{
public StickOfJoy() {
super("Stick of Joy");
dmg_fire = 2;
}
}
public class ArmorOfPity extends Armor{
public ArmorOfPity()
{
super("Armor of Pity");
life = 30;
}
}
然后我有以下功能:
Hero.getObject (Objects_class obj)
{
if (obj.getType == Objects_type.WEAPON)
....
}
我希望能够将Objects_class obj视为武器,但当然不可能(将一个母亲投给它的孩子)所以它让我觉得我的继承结构很糟糕。
我该怎么办?
答案 0 :(得分:1)
不需要Objects_type,
,因为Java中的对象知道它们是什么类型,并且可以使用instanceof
运算符测试它们的类型。你说你不能把“一个母亲”投射到它的孩子身上,但是可以将一个物体转向一个儿童类型。一般情况下,它可能会抛出ClassCastException,
,但如果您先使用instanceof,
进行测试,则不会发生此事。
public class Objects_class {
protected String name;
public Objects_class(String name) {
this.name = name;
}
}
public class Armor extends Objects_class {
int life = 0;
int res_fire = 0;
public Armor(String name, int largeur, int hauteur) {
super(name);
}
}
public class Weapon extends Objects_class {
protected int dmg_fire = 0;
public Weapon(String name) {
super(name);
}
}
public class Hero {
public void getObject(Objects_class obj) {
if (obj instanceof Weapon) {
Weapon weapon = (Weapon) obj;
wield(weapon);
}
if (obj instanceof Armor) {
Armor armor = (Armor) obj;
wear(armor);
}
}
}
我已经从类中删除了abstract
修饰符,因为不需要它,但是您可能希望它确保从不实例化这些基类。此外,我会将Objects_class
的名称更改为类似Item
的名称,因为对象和类具有可能导致混淆的特定含义。我还会将Hero的getObject
方法重命名为pickUpItem
,因为它不是Java语义上的getter。
答案 1 :(得分:1)
大卫康拉德有一些好点,我建议你读完,我不会在这里重复,但这是我会怎么做。
假设你的游戏世界中有一个角色正在游戏中拾取物品,那么可能会有很多不同的物品,有些物品在行为方面彼此如此不同,因此他们需要创建一个新的子类(比如拾取靴子和拾取物品)向上的翅膀)。
一旦你拿起一件物品,你可以选择让英雄尝试看看拾取了什么样的物品(例如,实例,枚举,等等),或者你可以让物品找出应该去的地方。
这是一个简化示例,其中玩家只有两个库存槽,一个武器和一个装甲。请注意,简单地添加一个新项目(如健康药水或超级新特殊武器)是多么容易,而无需更改播放器中的任何内容或进行投射。
public abstract class Item {
private int ID;
private static int IDCounter;
private String name;
public Item(String name) {
this.name = name;
this.ID = IDCounter;
IDCounter++;
}
public int getID() {
return ID;
}
public String getName() {
return name;
}
public abstract void attachToPlayer(Player player);
}
public class Armor extends Item {
private int life;
private int res_fire;
public Armor(String name) {
super(name);
}
@Override
public void attachToPlayer(Player player) {
// Only equip if upgrade
if (player.getArmor().res_fire > this.res_fire)
player.setArmor(this);
}
}
public class Weapon extends Item {
private int dmg_fire;
public Weapon(String name) {
super(name);
}
// ...stuff
@Override
public void attachToPlayer(Player player) {
// Only equip this if upgrade? You decide the logic
if(player.getWeapon().dmg_fire>this.dmg_fire)
player.setWeapon(this);
}
}
public class SuperSpecialWeapon extends Weapon {
private float bonusHealthModifier = 1.0f;
public SuperSpecialWeapon(String name) {
super(name);
}
@Override
public void attachToPlayer(Player player) {
// This bonus adds +100%HP bonus to the player!
int hp = (int) ((1 + bonusHealthModifier) * player.getHealth());
player.setHealth(hp);
player.setWeapon(this);
}
}
public class Potion extends Item {
private int health = 100;
public Potion() {
super("HealthPotion");
}
@Override
public void attachToPlayer(Player player) {
// If the player has room for one more potion, pick this up
Potion[] potions = player.getHealthPotions();
for (int i = 0; i < potions.length; i++) {
if(potions[i]==null){
potions[i] = this;
break;
}
}
}
// ..other stuff
}
最后是玩家
public class Player {
private Armor armor;
private Weapon weapon;
private String name;
private Potion[] healthPotions = new Potion[10];
private int health;
public Player(String name) {
this.name = name;
}
public Armor getArmor() {
return armor;
}
public Weapon getWeapon() {
return weapon;
}
public void setWeapon(Weapon weapon) {
this.weapon = weapon;
}
public void setArmor(Armor armor) {
this.armor = armor;
}
public void setHealth(int health) {
this.health = health;
}
public int getHealth() {
return health;
}
public Potion[] getHealthPotions() {
return healthPotions;
}
}