1

我正在制作一个小游戏,英雄的物品栏里装满了物品。

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 视为武器,但当然这是不可能的(将母亲投给孩子)所以这让我觉得我的继承结构很糟糕。

我应该怎么做?

4

2 回答 2

1

大卫康拉德有一些我建议你通读的好点,我不会在这里重复,但我会这样做。

假设你有一个角色在你的游戏世界中四处漫游拾取物品,可能有许多不同的物品,其中一些在行为上彼此如此不同,以至于需要创建一个新的子类(比如拾起靴子与拾起翅膀) .

一旦你拿起一个物品,你可以选择让英雄尝试看看你拿起了什么类型的物品(instanceof、枚举等),或者你可以让物品弄清楚它应该去哪里。

这是一个简化的示例,其中玩家只有两个库存槽,一个武器和一个盔甲。请注意,无需更改播放器中的任何内容或进行施法,只需将新物品(如健康药水或 superdupernewspecialweapon)添加到组合中是多么容易。

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;
    }

}
于 2013-10-20T00:48:13.643 回答
1

不需要,Objects_type,因为 Java 中的对象知道它们是什么类型,并且可以使用instanceof运算符来测试它们的类型。您说您不能将“a mother to its child”强制转换为“a mother to its child”,但可以将对象向下转换为子类型。一般来说,它可能会抛出一个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。

于 2013-10-19T18:47:56.907 回答