import java.lang.reflect.Method; public class ReflectionExample { public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName("HelloWorld"); Object helloWorld = clazz.newInstance(); Method sayHelloMethod = clazz.getMethod("sayHello"); sayHelloMethod.invoke(helloWorld); } } public class CustomClassLoader extends ClassLoader { @Override protected Class<?> findClass(String name) throws ClassNotFoundException { } } public class Main { public static void main(String[] args) throws Exception { ClassLoader customClassLoader = new CustomClassLoader(); Class<?> clazz = customClassLoader.loadClass("HelloWorld"); Object helloWorld = clazz.newInstance(); Method sayHelloMethod = clazz.getMethod("sayHello"); sayHelloMethod.invoke(helloWorld); } }


上一篇:
下一篇:
切换中文