2019-4-23 反射学习笔记

//1.通过权限定类名获得
        try {
            Class<?> studentClazz1 = Class.forName("com.reflact.Student");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //2.通过Java类型获得
        Class<Student> studentClass1 = Student.class;
        //3.通过实例对象获得
        Class<? extends Student> studentClazz = new Student().getClass();
try {
            Class<?> studentClazz = Class.forName("com.reflact.Student");
            //可以获得指定形参的构造方法
            Constructor<?> studentClazzConstructor =
                    studentClazz.getDeclaredConstructor(int.class,String.class);
        //获得指定的形参构造方法后初始化对象
            Student student = (Student)studentClazzConstructor.newInstance(1,"小明");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
//直接通过类对象创建实例
    @Test
    public void run2(){
        Class<Student> studentClass = Student.class;
        try {
            Student student = studentClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
//强制访问私有方法,创建实例
    @Test
    public void run3(){
        Class<Student> studentClass = Student.class;
        try {
            Constructor<Student> studentConstructor
                    = studentClass.getDeclaredConstructor(int.class);
            studentConstructor.setAccessible(true);//开启强制访问
            Student student
                    = studentConstructor.newInstance(1);
            student.setName("小明");
            System.out.println(student);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
 @Test
    public void run4(){
        //获得类对象
        Class<Student> studentClass = Student.class;
        try {
            //创建实例
            Student student = studentClass.newInstance();
            //获得方法
            Method setIdMethod = studentClass.getMethod("setId", int.class);
            //通过实例调用方法
            setIdMethod.invoke(student,1);
            Method getIdMethod = studentClass.getMethod("getId");
            int id = (int)getIdMethod.invoke(student);
            System.out.println(id);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
//获得私有字段
    @Test
    public void run5(){
        Class<Student> studentClass = Student.class;
        try {
            Student student = studentClass.newInstance();
            Field idField = studentClass.getDeclaredField("id");
            idField.setAccessible(true);
            idField.set(student,1);
            int id = (int)idField.get(student);
            System.out.println(id);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

 

posted @ 2019-04-23 17:55  于陆  阅读(174)  评论(0编辑  收藏  举报