How do I properly declare an instance of a subclas

2019-06-17 07:06发布

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.

3条回答
看我几分像从前
2楼-- · 2019-06-17 07:13

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();
查看更多
Rolldiameter
3楼-- · 2019-06-17 07:18

"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();
查看更多
一纸荒年 Trace。
4楼-- · 2019-06-17 07:31

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();
查看更多
登录 后发表回答