[英]How do I properly declare an instance of a subclass?
I am currently making a text based adventure in Java for the purposes of using it a test platform, to try out new things I learn from this Java book I'm reading. 我目前正在使用Java进行基于文本的冒险,以便将其用作测试平台,尝试从我正在阅读的这本Java书中学到的新东西。
I am now trying to declare an instance of a subclass (as the player is scripted to find it). 我现在正在尝试声明一个子类的实例(因为播放器的脚本是为了找到它)。 The parent class is
Item
and it has two subclasses: Weapon
and Armour
. 父类是
Item
,它有两个子类: Weapon
和Armour
。
However, no matter which way I try and declare it in, the IDE I'm using (Eclipse) flags the line with the following error: 但是,无论我尝试和声明它的方式,我正在使用的IDE(Eclipse)标记该行,并出现以下错误:
No enclosing instance of type Item is accessible.
不能访问类型为Item的封闭实例。 Must qualify the allocation with an enclosing instance of type Item (egxnew A() where x is an instance of Item).
必须使用Item类型的封闭实例限定分配(例如,xx是A(),其中x是Item的实例)。
When I attempt to declare it like any of the following: 当我尝试将其声明为以下任何一项时:
Item machinePistol = new Weapon();
Weapon machinePistol = new Weapon();
Item machinePistol = new Item.Weapon();
Weapon machinePistol = new Item.Weapon();
For reference the item class looks like this: 作为参考,item类看起来像这样:
package JavaAIO;
public class Item
{
public String itemName;
public double itemWeight;
public class Weapon extends Item
{
public double damage;
public double speed;
}
public class Armour extends Item
{
public double dmgResist;
public double attSpdMod;
}
}
So if anyone could tell me how I could properly instantiate a Weapon (so I can set the values of its fields and give it to the player), I would greatly appreciate it. 所以如果有人能告诉我如何正确地实例化武器(所以我可以设置其字段的值并将其交给玩家),我将非常感激。
"No enclosing instance of type Item is accessible. Must qualify the allocation with an enclosing instance of type Item (egxnew A() where x is an instance of Item) ."
“无法访问类型Item的封闭实例。必须使用类型为Item的封闭实例限定分配(例如, xnew A(),其中x是Item的实例) 。”
It's pretty self-explaining: 这是非常自我解释:
Item machinePistol = new Item().new Weapon();
Or: 要么:
Item item = new Item();
Item machinePistol = item.new Weapon();
However, I strongly recommend to put them in their own classes so that you can end up with: 但是,我强烈建议将它们放在自己的类中,以便最终得到:
Item machinePistol = new Weapon();
To do this: 去做这个:
Item machinePistol = new Item();
Weapon machinePistol = new Item.Weapon();
you would want to declare your inner classes as static
: 你想要将你的内部类声明为
static
:
public static class Weapon extends Item
{
public double damage;
public double speed;
}
public static class Armour extends Item
{
public double dmgResist;
public double attSpdMod;
}
Or another (and better) option is to not make them inner classes: 或者另一个(更好的)选择是不要让它们成为内部类:
public class Item {}
public class Weapon extends Item {}
public class Armour extends Item {}
Then you can make them like this; 然后你就可以这样做了;
Item machinePistol = new Item();
Item machinePistol = new Weapon();
Item someArmour = new Armour();
You shouldn't declare the classes Weapon
and Armour
inside Item
. 你不应该在
Item
声明类Weapon
和Armour
。 Just declare them outside : 只需在外面宣布:
public class Item {
public String itemName;
public double itemWeight;
}
public class Weapon extends Item {
public double damage;
public double speed;
}
public class Armour extends Item {
public double dmgResist;
public double attSpdMod;
}
So you can instantiate them like this: 所以你可以像这样实例化它们:
Weapon machinePistol = new Weapon();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.