Java:实例化抽象类的特定子类的方法
如何根据运行时生成的classNameString创建抽象类的特定子类的对象?假设有一个抽象类 A,
public abstract class A {
abstract protected void method();
A getNewInstance() throws InstantiationException, IllegalAccessException{
return this.getClass().newInstance();
}
}
有 N 个子类,即 A1、A2、..、AN。需要编写以下方法,该方法将返回基于 classNameString 的子类对象
A getSubClassObject(String classNameString)
我有以下两个丑陋实现 第一:
A getSubClassObject(String classNameString){
A obj = null;
if(classNameString.equals("A1")){
obj = new A1();
}else if(classNameString.equals("A2")){
obj = new A2();
}
...
}else if(classNameString.equals("AN")){
obj = new AN();
}
return obj;
}
第二:
A getSubClassObject(String classNameString){
A obj = null;
try {
obj = this.subClassObjectsHashMap().get(classNameString).getNewInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return obj;
}
private HashMap<String, A> subClassObjectsHashMap(){
HashMap<String, A> subClassObjectsHashMap = new HashMap<String,A>();
subClassObjectsHashMap.put("A1", new A1());
subClassObjectsHashMap.put("A2", new A2());
....
subClassObjectsHashMap.put("AN", new AN());
return subClassObjectsHashMap;
}
有没有更好的方法来解决这个问题?
How to create an object of a particular sub-class of an abstract class based on the classNameString generated on runtime? Let say there is an abstract class A
public abstract class A {
abstract protected void method();
A getNewInstance() throws InstantiationException, IllegalAccessException{
return this.getClass().newInstance();
}
}
Let there be N sub-classes viz A1, A2,.., AN. There is a need to write following method which would return a subclass object based on classNameString
A getSubClassObject(String classNameString)
I have following two ugly implementations
First:
A getSubClassObject(String classNameString){
A obj = null;
if(classNameString.equals("A1")){
obj = new A1();
}else if(classNameString.equals("A2")){
obj = new A2();
}
...
}else if(classNameString.equals("AN")){
obj = new AN();
}
return obj;
}
Second:
A getSubClassObject(String classNameString){
A obj = null;
try {
obj = this.subClassObjectsHashMap().get(classNameString).getNewInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return obj;
}
private HashMap<String, A> subClassObjectsHashMap(){
HashMap<String, A> subClassObjectsHashMap = new HashMap<String,A>();
subClassObjectsHashMap.put("A1", new A1());
subClassObjectsHashMap.put("A2", new A2());
....
subClassObjectsHashMap.put("AN", new AN());
return subClassObjectsHashMap;
}
Are there any better ways to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
类的事情怎么样?
进行适当的错误处理之
what about doing something like
with appropriate error handling?
是的,如果所有构造函数都接收相同的参数(在您的示例中,没有参数),您可以执行
这两种方法都可以抛出各种异常,因此您需要添加一些 catch 块。
Yes, if all the constructors receive the same parameter (in your example, no paramaters) you can do
Both methods can throw various exceptions, so you need to add some catch blocks.
查看 Java API 中的 java.lang.Class、forName(String) 和 newInstance() 方法
Look in the Java API at java.lang.Class, methods forName(String) and newInstance()