java 如何正确声明子类的实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4413000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:15:23  来源:igfitidea点击:

How do I properly declare an instance of a subclass?

javasubclassinstantiation

提问by James

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 Itemand it has two subclasses: Weaponand Armour.

我现在试图声明一个子类的实例(因为播放器被编写为找到它)。父类是Item,它有两个子类:WeaponArmour

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. Must qualify the allocation with an enclosing instance of type Item (e.g. x.new A() where x is an instance of Item).

无法访问 Item 类型的封闭实例。必须使用 Item 类型的封闭实例限定分配(例如 xnew 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:

作为参考,项目类如下所示:

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.

因此,如果有人能告诉我如何正确实例化武器(以便我可以设置其字段的值并将其提供给玩家),我将不胜感激。

回答by BalusC

"No enclosing instance of type Item is accessible. Must qualify the allocation with an enclosing instance of type Item (e.g. x.new 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();

回答by Joel

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

回答by Hosam Aly

You shouldn't declare the classes Weaponand ArmourinsideItem. Just declare them outside:

你不应该声明 classesWeaponArmourinsideItem。只需在外面声明它们:

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