Java - 一般处理子类的创建
我有三个非常相似的类,除了一个方法。因此,我选择将它们的其余功能放入抽象超类中。然而,当谈到创建这些类的实例时,我不知道如何实现在我看来“显而易见”或“优雅”的方法。它们的构造函数本质上是相同的,并且我需要每个实例的多个实例,因此我想做类似以下的事情:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
海事组织,是的。
您可以执行以下操作:
array.getClass()
获取数组的类,getConmponentType()
获取数组的基类型newInstance( )
创建实例...但这会导致代码脆弱,就像用大锤敲碎核桃一样。
IMO, yes.
You could do the following:
array.getClass()
to get the class of the array,getConmponentType()
to get the array's base typenewInstance()
to create an instance... but this results in fragile code and is like using a sledge-hammer to crack a walnut.
您可以在内部循环中使用反射,例如 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)您可以创建静态方法,
无需创建新实例
SubclassA.getNewInstances(3)
即可访问静态方法。在矩阵的定义中,您需要首先定义第一个维度(因此它变为
new Superclass[3][]
You can create an static method
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][]