简体   繁体   English

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

[英]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 ,它有两个子类: 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. 不能访问类型为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声明类WeaponArmour 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.

相关问题 如何在Java中动态声明实例的泛型类型 - How do I declare the generic type of an instance dynamically in java 通过子类实例调用时,如何在子类的方法中使用子类的静态字段? - How do I use a subclass' static fields in superclass' method when calling via instance of subclass? 在Java中,如何使子类的实例变量具有子类的类型,而不是父类的类型? - In Java, how do I make instance variables of a subclass have the type of the subclass, rather than of the superclass? 如何声明包含实现接口的类的子类的变量? - How do I declare a variable that contains a subclass of a class which implements an interface? 如何检查我是否使用子类的实例? - How to check that I work with instance of subclass? 如何创建泛型子类的实例? 获取错误:“绑定不匹配:类型...不是有界参数的有效替代...” - How do I make an instance of generic subclass? Getting error: “Bound mismatch: The type … is not a valid substitute for the bounded parameter …” 如何在java中声明子类构造函数 - How to declare subclass constructor in java 如何声明JComboBox? - How do I declare a JComboBox? 如何正确地泛化泛型? - How to subclass generics properly? Java-如何为子类的子类调用复制构造函数? - Java - how do I call copy constructor for subclass of subclass?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM