简体   繁体   English

聚合子类中的Java构造函数

[英]Java constructor in agregation subclass

I have recently learnt the use of subclasses and I am creating a Java game. 我最近学习了子类的用法,并且正在创建Java游戏。 I have a superclass CHARACTER which is the character of the game. 我有一个超类CHARACTER,这是游戏的角色。 This class has many subclasses like SOLDIER and FARMER. 此类具有许多子类,例如SOLDIER和FARMER。 Now I need to have a subclass called GROUP, which is a bunch of characters. 现在,我需要一个名为GROUP的子类,它是一堆字符。

The superclass constructor is the following: 超类构造函数如下:

public Character (String id,Position p);

How can I create the constructor of the subclass GROUP, which has to call the super constructor N times? 如何创建子类GROUP的构造函数,该子类必须调用超级构造函数N次?

How can I create the constructor of the subclass GROUP, which has to call the super constructor N times? 如何创建子类GROUP的构造函数,该子类必须调用超级构造函数N次?

You can't. 你不能 And you don't need to. 而且您不需要。

Each Java class has exactly one superclass. 每个Java类只有一个超类。 And each object of a class has only one state of its superclass. 一个类的每个对象只有其超类的一个状态。

Fortunately, your Group class does not have to call the super constructor N times. 幸运的是,你的组类不必调用超构造N次。 Either a Group is a Character or not. 组是否为角色。 If it is a Character, it is one character. 如果它是一个字符,则它是一个字符。 You call the Group's super constructor one time. 您一次调用了组的超级构造函数。

Regardless, the Group contains characters. 无论如何,该组都包含字符。 The super constructor of a Soldier or Farmer is called from the constructor for the Soldier or Farmer -- not from the Group/s that contain the character. 士兵或农夫的超级构造函数是从士兵或农夫的构造函数调用的,而不是从包含角色的组中调用的。

For example, your Farmer class might look like this: 例如,您的Farmer类可能如下所示:

public class Farmer extends Character {
   public Farmer(String id,Position p) {
      super( id, p ); // <-- Superclass constructor for one farmer.
      ...
   }
   ...
}

And your Group class might look like this, if a Group is a Character: 如果Group是Character,那么您的Group类可能看起来像这样:

class Group extends Character { 
    private Collection<Character> m_members;
    Group( String id,Position p, Collection<Character> members ) {
        super( id, p ); // Superclass constructor for the *group*.
        m_members = new ArrayList<>( members ); // Defensive copy
    } 
}

Or this, if it's not. 或者,如果不是这样的话。

class Group  { 
    Group( Collection<Character> members ) {
        m_members = new ArrayList<>( members ); // Defensive copy
    }
}

Your group should rather be an array, a list or similar. 您的组应该是数组,列表或类似名称。 Then you instantiate via a loop. 然后,您通过循环实例化。

CHARACTER[] group = new CHARACTER[10];
for (int c = 0; c < 10; c++) group[i] = new CHARACTER(id, position);

That way you also can access the members easily. 这样,您还可以轻松访问成员。 Becaues FARMERS and SOLDIERS are subclassed to CHARACTER, 由于FARMERS和SOLDIERS被子类化为CHARACTER,

CHARACTER[] group = new CHARACTER[10];
for (int c = 0; c < 10; c++) group[i] = new SOLDIER(id, position);

Will also work. 也可以。

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

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