构造函数泛型问题

时间:2015-02-26 15:07:34

标签: java eclipse generics

我正在制作一个基于文本的小游戏以获得乐趣,而且我正在搞乱泛型并遇到了一个问题。

我有一个类Equippable:

public class Equippable extends Item
{
    double mDurability; 

    public Equippable(String name, double weight, double durability)
    {
        super(name, weight);
        mDurability = durability;
    }//Equippable

    //Getters
    public double getDurability(){return mDurability;}

    //Setters
    public void setDurability(double Durability){mDurability = Durability;} 
}//Equippable

我有一个名为BodyPart的类的构造函数,我想接受Equippable的任何子类作为参数:

public BodyPart(String name,  Class<? extends Equippable> equipClass)
{
    mName         = name;
    mEquipClass   = equipClass;
}//BodyPart

Equippable有一个子类Armor。

public class Armor extends Equippable
{
    double mArmorRating;

    public Armor(String name, double weight, double durability, double armorrating) 
    {
        super(name, weight, durability);
        mArmorRating = armorrating;
    }//Armor
}//Armor

最后,我有几个扩展Armor.class的类。我不会发布所有内容,因为它们在这一点上是相同的,除了类的名称。

public class HumanoidBodyArmor extends Armor
{

    public HumanoidBodyArmor(String name, double weight, double durability, double armorrating) 
    {
        super(name, weight, durability, armorrating);
    }//HumanoidBodyArmor
}//HumanHeadArmor

当我这样做时:

BodyPart bp = new BodyPart("Head"  , Armor.class));

一切都很好,并且创建了对象。但是当我做的时候

BodyPart bp = new BodyPart("Head"  , HumanHeadArmor.class));

Eclipse抛出一个契合并说

The constructor BodyPart(String, Class<HumanoidHeadArmor>) is undefined

我很确定这会起作用,但显然我错过了一些东西。有人可以对这个问题有所了解吗?

为了理智,我做了

Equippable.class.isAssignableFrom(HumanoidHeadArmor.class) 

确实评估为真。

修改的 根据评论中的要求,这里是HumanoidBodyArmor类

public class HumanoidBodyArmor extends Armor
{

    public HumanoidBodyArmor(String name, double weight, double durability, double armorrating) 
    {
        super(name, weight, durability, armorrating);
    }//HumanoidBodyArmor
}//HumanoidBodyArmor

headarmor / bodyarmor / footarmor之间没有区别。除了这个名字,你们都是完全相同的。只需要使用相同构造函数的空类来调用它们的父级(Armor)构造函数。

2 个答案:

答案 0 :(得分:0)

您没有正确使用generics

首先,Equippable可能应该是interface而不是类,而Armor也可以是interface,其范围为Equippable

然后你可以

public class BodyPart {

    public BodyPart(String name,  Equipable equip)
    {
        mName         = name;
        mEquip   = equip;
    }
}

and

public class Head extends BodyPart {
    public Head(String name, HumanoidHeadArmor equippable) {
      super(name, equippable);
    }
}

答案 1 :(得分:0)

这一切对我来说都很好 - 我怀疑你有一些命名问题。

class Item {

    public Item(String name, double weight) {

    }
}

public class Equippable extends Item {

    double mDurability;

    public Equippable(String name, double weight, double durability) {
        super(name, weight);
        mDurability = durability;
    }

    //Getters
    public double getDurability() {
        return mDurability;
    }

    //Setters
    public void setDurability(double Durability) {
        mDurability = Durability;
    }
}

class BodyPart {

    String name;
    Class<? extends Equippable> equipClass;

    public BodyPart(String name, Class<? extends Equippable> equipClass) {
        this.name = name;
        this.equipClass = equipClass;
    }
}

public class Armor extends Equippable {

    double mArmorRating;

    public Armor(String name, double weight, double durability, double armorrating) {
        super(name, weight, durability);
        mArmorRating = armorrating;
    }
}

public class HumanoidBodyArmor extends Armor {

    public HumanoidBodyArmor(String name, double weight, double durability, double armorrating) {
        super(name, weight, durability, armorrating);
    }
}

public void test() {
    BodyPart head = new BodyPart("Head", Armor.class);
    BodyPart body = new BodyPart("Body", HumanoidBodyArmor.class);
}