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

发布于 2024-10-06 18:14:10 字数 997 浏览 0 评论 0原文

我目前正在使用 Java 进行基于文本的冒险,目的是使用它作为测试平台,尝试我从正在阅读的这本 Java 书中学到的新东西。

我现在尝试声明一个子类的实例(因为玩家编写了脚本来查找它)。 父类是 Item,它有两个子类:WeaponArmour

但是,无论我尝试以哪种方式声明它,我正在使用的 IDE (Eclipse) 都会将该行标记为以下错误:

没有可访问的 Item 类型的封闭实例。必须使用 Item 类型的封闭实例来限定分配(例如 xnew A(),其中 x 是 Item 的实例)。

当我尝试像以下任何一个一样声明它时:

Item machinePistol = new Weapon(); 
Weapon machinePistol = new Weapon();
Item machinePistol = new Item.Weapon();
Weapon machinePistol = new Item.Weapon();

作为参考,项目类看起来像这样:

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

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

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.

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.

However, no matter which way I try and declare it in, the IDE I'm using (Eclipse) flags the line with the following error:

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).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杯别 2024-10-13 18:14:10

“没有可访问的 Item 类型的封闭实例。必须使用 Item 类型的封闭实例来限定分配(例如 xnew A(),其中 x 是 Item 的实例)。”

这是非常不言自明的:

Item machinePistol = new Item().new Weapon(); 

或者:

Item item = new Item();
Item machinePistol = item.new Weapon(); 

但是,我强烈建议将它们放在自己的类中,以便您最终可以得到:

Item machinePistol = new Weapon();

"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)."

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();
绅刃 2024-10-13 18:14:10

为此:

Item machinePistol = new Item();
Weapon machinePistol = new Item.Weapon();

您需要将内部类声明为静态:

 public static class Weapon extends Item
 {

  public double damage;
  public double speed;

 }

 public static class Armour extends Item
 {
  public double dmgResist;
  public double attSpdMod; 

 }

或者另一个(也是更好的)选择是不将它们设为内部类:

public class Item {}
public class Weapon extends Item {}
public class Armour extends Item {}

然后您可以将它们设为这样;

Item machinePistol = new Item();
Item machinePistol = new Weapon();
Item someArmour = new Armour();

To do this:

Item machinePistol = new Item();
Weapon machinePistol = new Item.Weapon();

you would want to declare your inner classes as 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();
小巷里的女流氓 2024-10-13 18:14:10

您不应在 Item 内声明 WeaponArmour 类。只需在外部声明它们即可:

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

这样您就可以像这样实例化它们:

Weapon machinePistol = new Weapon();

You shouldn't declare the classes Weapon and Armour inside Item. 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文