public class ReflectDemo02 { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { //获取Class对象 Class<?> c = Class.forName("com.reflect_02.Student"); //基本数据类型也是可以.class的 Constructor<?> con = c.getConstructor(String.class, int.class, String.class); Object obj = con.newInstance("林青霞", 30, "西安"); System.out.println(obj); } }
运行结果:(Student类重写toString方法,详细代码见 反射获取构造方法并使用 博文)
原文:https://www.cnblogs.com/pxy-1999/p/13170553.html