代码
提交代码
import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class ReflectionDemo { public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { // 获取构造方法 Constructor constructor = StringBuffer.class.getConstructor(String.class); // 调用构造方法 Object str = constructor.newInstance("Hello Imooc"); System.out.println(str); } }
运行结果