Java - 一般处理子类的创建

发布于 2024-12-07 02:19:25 字数 900 浏览 0 评论 0原文

我有三个非常相似的类,除了一个方法。因此,我选择将它们的其余功能放入抽象超类中。然而,当谈到创建这些类的实例时,我不知道如何实现在我看来“显而易见”或“优雅”的方法。它们的构造函数本质上是相同的,并且我需要每个实例的多个实例,因此我想做类似以下的事情:

private SubclassA[] subclassA_array, SubclassB[] subclassB_array, SubclassC[] subclassC_array;
subclassA_array = new SubclassA[getNumInstancesOfClassANeeded()]
subclassB_array = new SubclassA[getNumInstancesOfClassBNeeded()]
subclassC_array = new SubclassA[getNumInstancesOfClassCNeeded()]

// might have my syntax wrong here; array of the three subclass arrays
private Superclass[][] superArray = new Superclass[][3];
superArray[0] = subclassA_array;
superArray[1] = subclassA_array;
superArray[2] = subclassA_array;
for ( Superclass[] array: superArray )
    for(int i = 0; i< array.length; i++)
      //  array[i] = new..... what goes here?
    }
}

如何找到在最内层循环中构造的适当的类?这实际上是一种非常奇怪的解决问题的方法吗?我错过了一些更明显的事情吗?我应该说“见鬼去吧!”并且只有三个独立的循环?

I have three classes that are quite similar, excepting a single method. Therefore, I chose to put the rest of their functionality into an abstract superclass. When it comes to creating instances of these classes, however, I'm at a loss for how to implement what seems to me like the "obvious" or "elegant" approach. Their constructors are essentially identical, and I need multiple instances of each, so I wanted to do something like the following:

private SubclassA[] subclassA_array, SubclassB[] subclassB_array, SubclassC[] subclassC_array;
subclassA_array = new SubclassA[getNumInstancesOfClassANeeded()]
subclassB_array = new SubclassA[getNumInstancesOfClassBNeeded()]
subclassC_array = new SubclassA[getNumInstancesOfClassCNeeded()]

// might have my syntax wrong here; array of the three subclass arrays
private Superclass[][] superArray = new Superclass[][3];
superArray[0] = subclassA_array;
superArray[1] = subclassA_array;
superArray[2] = subclassA_array;
for ( Superclass[] array: superArray )
    for(int i = 0; i< array.length; i++)
      //  array[i] = new..... what goes here?
    }
}

How would I find the appropriate class to construct in that innermost loop? Is this actually a really oddball way of approaching the problem; have I missed something more obvious? Should I just say "to hell with it!" and just have three separate loops?

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

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

发布评论

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

评论(3

伪装你 2024-12-14 02:19:25

我应该说“见鬼去吧!”并且只有三个独立的循环?

海事组织,是的。


您可以执行以下操作:

  1. 使用 array.getClass() 获取数组的类,
  2. 使用 getConmponentType() 获取数组的基类型
  3. 使用 newInstance( ) 创建实例
  4. 将实例引用分配给数组。

...但这会导致代码脆弱,就像用大锤敲碎核桃一样。

Should I just say "to hell with it!" and just have three separate loops?

IMO, yes.


You could do the following:

  1. Use array.getClass() to get the class of the array,
  2. Use getConmponentType() to get the array's base type
  3. Use newInstance() to create an instance
  4. Assign the instance reference to the array.

... but this results in fragile code and is like using a sledge-hammer to crack a walnut.

请别遗忘我 2024-12-14 02:19:25

您可以在内部循环中使用反射,例如 array.getClass().getComponentType().newInstance() ,但我认为可能有更好的解决方案来解决整个问题(要回答这个问题,我需要有关您想要编码的内容的更多信息)

You could work with reflection in your inner loop, something like array.getClass().getComponentType().newInstance(), but I think there might be better solutions to the overall problem (To answer that, I'd need more information about what you want to code)

凝望流年 2024-12-14 02:19:25

您可以创建静态方法,

public static subclassA[] getNewInstances(int numberOfInstances);

无需创建新实例SubclassA.getNewInstances(3)即可访问静态方法。

在矩阵的定义中,您需要首先定义第一个维度(因此它变为 new Superclass[3][]

You can create an static method

public static subclassA[] getNewInstances(int numberOfInstances);

static methods can be accessed without the need to create a new instance SubclassA.getNewInstances(3).

In your definition of the matrix, you need to define first the first dimension (so it becomes new Superclass[3][]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文