Hi I have the following clases:
public abstract class A(){
public void doSomething();
public void makeMe(){
try {
A.class.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | IllegalArgumntException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class B() extends A{
@Override
public void doSomething(){
}
}
public class C() extends A{
@Override
public void doSomething(){
}
}
How can do makeMe for that I can get the follow behavior:
B.makeMe()
C.makeMe()
The original class are more complex than this, this is only an example. But how can get it this, is it possible?
It is possible do this with reflections?
Thanks
You're trying to instantiate the abstract class A
. To instantiate the child class ( B.makeMe()
creates a B
object and C.makeMe()
creates a C
object), replace A.class
with this.getClass()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.