您是将
Class对象作为参数传递还是作为的子类传递
Foo?
在这两种情况下,解决方案都几乎相同,您可以在Class对象上使用newInstance方法。
public Foo fooFactory(Class<? extends Foo> c){ Foo instance = null; try { instance = c.newInstance(); } catch (InstantiationException e) { // ... } catch (IllegalAccessException e) { // ... } return instance; // which might be null if exception occurred, // or you might want to throw your own exception}如果需要构造函数参数,则可以使用Class
getConstructor方法,然后从那里使用Constructor
newInstance(…)方法。



