废话不多说,直接上源码
public interface SayHello {
void hello();
}
import java.lang.reflect.Constructor;
public interface HelloCtor {
Constructor<SayHello> getConstructor();
}
public class HelloAA implements SayHello {
public void hello() {
System.out.println("hello");
}
}
public class HelloBB {
}
import java.lang.reflect.InvocationTargetException;
public class ParadigmTest {
public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
SayHello a = ParadigmTest.createSome((Class<HelloCtor>) Class.forName("HelloAA"));
a.hello();
}
public static <T extends SayHello> T createSome(Class<HelloCtor> Cls) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
T a = (T) Cls.getConstructor().newInstance();
return a;
}
}