简体   繁体   English

如何将不同的内部类添加到ArrayList?

[英]How do I add different inner classes to an ArrayList?

So, I'm building a very basic program based on the Warhammer 40k tabletop game as an extra credit assignment for my Comp Sci class. 因此,我正在基于Warhammer 40k桌面游戏构建一个非常基本的程序,作为我的Comp Sci类的额外功课。 I get to develop the specs and then I implement the specs. 我先制定规范,然后再实施规范。 In this game, I have a class Army that has an ArrayList of class Units: 在这个游戏中,我有一个阶级军,具有一个单位类别的ArrayList:

List<Units> unitsList = new ArrayList<Units>();

I've created the class Units with innerclasses that contain the different unit types. 我用包含不同单元类型的内部类创建了单元类。 So, a code snippet from that would be: 因此,其中的代码片段为:

public class Units 
{
   int strength, toughness, attacks, save, wounds;
   class TroopUnit
   {
      public static final int strength = 3;
      public static final int toughness = 3;
      public static final int attacks = 1;
      public static final int save = 5;

      public int wounds = 1;
   }

   class EliteTroopUnit
   {
      public static final int strength = 3;
      public static final int toughness = 3;
      public static final int attacks = 1;
      public static final int save = 4;

      public int wounds = 1;
   }

}

There are more unit types, but I think you can get the idea. 单元类型更多,但我想您可以理解。 I've tried adding these different innerclasses of Units inside the Units ArrayList: 我试过在Units ArrayList内添加这些不同的Units内部类:

public Army(int troopPoints, int eliteTroopPoints, int commandPoints,
     int commandTroopPoints, int fastAttackPoints, int heavyAttackPoints)
{      
   for (int i = 0; i < (troopPoints/TROOP_COST); i++)
      if (troopPoints % TROOP_COST == 0) 
      {
         if (points < MAX_POINTS) 
         {
            unitsList.add(new Units.TroopUnit());
            points += TROOP_COST;
            numUnits++;
         }
      }
   for (int i = 0; i < (eliteTroopPoints/ELITE_TROOP_COST); i++)
      if (eliteTroopPoints % ELITE_TROOP_COST == 0) 
      {
         if (points < MAX_POINTS) 
         {
            unitsList.add(new Units.EliteTroopUnit());
            points += ELITE_TROOP_COST;
            numUnits++;
         }
      }
}

However, I run into an issue, because I have to declare and instantiate the ArrayList with a specific type of innerclass in order to add an inner class to it. 但是,我遇到了一个问题,因为我必须使用特定类型的内部类声明并实例化ArrayList才能向其添加内部类。 For example, List<Units.someUnitInnerClass> unitsList = new ArrayList<Units.someUnitInnerClass(); 例如, List<Units.someUnitInnerClass> unitsList = new ArrayList<Units.someUnitInnerClass();

So, is there a workaround for this problem where I can add any of the innerclasses to the ArrayList? 因此,是否有解决此问题的方法,可以在其中将任何内部类添加到ArrayList中? Am I going about this problem incorrectly? 我会错误地解决此问题吗? If so, what are other alternatives? 如果是这样,还有哪些其他选择? Thank you! 谢谢!

EDIT: I originally had listed this as "How do I add different subclasses to an ArrayList, but that was a mistake (corrected by Rudi Kershaw). 编辑:我最初将其列为“如何将不同的子类添加到ArrayList,但这是一个错误(由Rudi Kershaw纠正)。

In the example code you gave, TroopUnit and EliteTroopUnit are not subtypes of Unit . 在您提供的示例代码中, TroopUnitEliteTroopUnit 不是 Unit子类型。 They are inner classes . 他们是内部阶级

If you want them to be subclasses of Unit , they need to extend Unit . 如果希望它们成为Unit子类,则需要扩展 Unit For example; 例如;

public class Unit {}
public class TroopUnit extends Unit {}

In the example above TroopUnit extends (and is therefore a subclass of) Unit . 在上面的示例中, TroopUnit扩展(因此是Unit的子类)。 From a quick glance, it looks as though you could just add extends Unit to the end of your inner class declarations and it should do as you intend. 乍一看,您似乎可以只将extends Unit添加到内部类声明的末尾,并且它应该按预期进行。

I hope this helps. 我希望这有帮助。

References & Further Reading: 参考资料和进一步阅读:

First of all, a subclass should extends a class. 首先,子类应该扩展一个类。 like this: 像这样:

public class TroopUnit extends Units{}

Second of all, answer to your title question, which is irrelevant to your code problem, is that you should get a class above in hierarchy than your object which you are adding to list, and because in java Object is parent of all Classes then you can instantiate your List like this: 第二,回答标题问题,这与代码问题无关,是您应该在层次结构上获得比要添加到列表中的对象更高的类,并且因为在Java中Object是所有类的父类,所以您可以像这样实例化您的列表:

List<Object> unitsList = new ArrayList<Object>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM