gpt4 book ai didi

java - 构造函数泛型问题

转载 作者:太空宇宙 更新时间:2023-11-04 14:07:08 24 4
gpt4 key购买 nike

我正在制作一个基于文本的小游戏,只是为了好玩,我正在摆弄泛型并遇到了问题。

我有一个可装备的类(class):

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) 

它确实评估为 true。

编辑根据评论中的要求,这里是 HumanoidBodyArmor 类

public class HumanoidBodyArmor extends Armor
{

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

头甲/ body 甲/足甲之间没有区别。除了名字之外,它们都是相同的。只是具有相同构造函数的空类,仅调用其父级 (Armor) 构造函数。

最佳答案

这对我来说一切正常 - 我怀疑你有一些命名问题。

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

关于java - 构造函数泛型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28745896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com