反射获取构造方法并使用练习2

public class ReflectDemo03 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //获取Class对象
        Class<?> c = Class.forName("com.reflect_02.Student");

        Constructor<?> con = c.getDeclaredConstructor(String.class);
        //取消访问检查
        con.setAccessible(true);
        Object obj = con.newInstance("林青霞");
        System.out.println(obj);
    }
}

运行结果:

猜你喜欢

转载自www.cnblogs.com/pxy-1999/p/13170677.html