我的代码是:
class ReferenceRangeBuilder<T extends DvOrderedBuilder> {
private DvTextBuilder meaningBuilder;
private DvIntervalBuilder<T> rangeBuilder;
public ReferenceRangeBuilder() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
}
}
我想启动rangeBuilder,以便可以在其中设置值。
我想在构造函数中执行此操作。
对于DvIntervalBuilder rangeBuilder我做到了:
public DvIntervalBuilder() throws NoSuchFieldException, InstantiationException, IllegalAccessException, ClassNotFoundException {
ParameterizedType pt = (ParameterizedType) getClass().getGenericSuperclass();
// You may need this split or not, use logging to check
String parameterClassName = pt.getActualTypeArguments()[0].toString().split("\\s")[1];
T lower = (T) Class.forName(parameterClassName).newInstance();
T upper = (T) Class.forName(parameterClassName).newInstance();
}
最佳答案
我自己编写了一个这样的构造函数来解决了这个问题,这要感谢所有宝贵的时间和意见。
我的解决方案是:
public ReferenceRangeBuilder(Class<T> claz) {
try {
lowerReferenceRange = claz.newInstance();
upperReferenceRange = claz.newInstance();
} catch (InstantiationException ex) {
Logger.getLogger(ReferenceRangeBuilder.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(ReferenceRangeBuilder.class.getName()).log(Level.SEVERE, null, ex);
}
}