zoukankan      html  css  js  c++  java
  • 反射

    public class Student  {
    
        public Integer id;
        String name;
        protected String sex;
        private Integer age;
        public Date birth;
        public Student() {
            System.out.println("===================");
        }
        public Student(int i) {
            System.out.println("===================");
        }
    }

    一.获取对应类的Class对象

        @Test 
        public void testGetClass() throws Exception {
            // 1. 通过类本身class属性获取
            System.out.println(Student.class);
            // 2. 通过类对应实例获取(自动补全CTRL+2,l)
            Student student = new Student(10);
            System.out.println(student.getClass());
            // 3. 通过Class类中提供静态forName方法获取
            Class clazz = Class.forName("com.demo.Student");
            System.out.println(clazz);
        }    

    二.获取一个Class对象中的成员变量

      @Test
        public void testGetClassForField() throws Exception {
            Class clazz = Student.class;
            // 获取所有的由public修饰的成员变量
            Field[] fields = clazz.getFields();
            for (Field field : fields) {
                System.out.println(field);
            }// 变量: 局部变量定义方法里面, 成员变量定义类的里面方法的外面
            // 获取当前Class对应类中所有定义的成员变量(定义类的里面,方法外面)
            fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                System.out.println(field);
                System.out.println("访问修饰符:" + Modifier.toString(field.getModifiers()));
                System.out.println("字段类型:" + field.getType().getTypeName());
                System.out.println("字段类型简单名称:" + field.getType().getSimpleName());
                System.out.println("字段名称:" + field.getName());
            }
            // 通过Class对象创建类实例,
            Object instance = clazz.newInstance(); // Object stu = new Student();
            // 单独获取一个字段
            Field field = clazz.getDeclaredField("age");
            // 开启任何字段访问权限
            field.setAccessible(true);
            // 设置私有字段的值
            field.set(instance, 25);
            // 通过反射获取字段的值
            /*
             * Object value = field.get(instance); System.out.println(value);
             */
            if (instance instanceof Student) {
                Student stu = (Student) instance;
                // System.out.println(stu.getAge());
            }
        }

    三.获取Class对象中所有方法及信息

      @Test 
        public void testClassForMethod() throws Exception {
            System.out.println("------- // 获取Class对象中所有方法及信息--------");
            Class clazz = Student.class;
            // 获取由public修饰的方法
            Method[] methods = clazz.getMethods();
            for (Method method : methods) {
                System.out.println(method);
            }
            System.out.println("----------------------------");
            // 获取当前类中所有的方法
            methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                System.out.println(method);
                System.out.println("方法访问修饰符:" + Modifier.toString(method.getModifiers()));
                System.out.println("方法返回类型:" + method.getReturnType().getSimpleName());
                System.out.println("方法名称:" + method.getName());
                System.out.println("参数类型数组:" + Arrays.toString(method.getParameterTypes()));
                System.out.println("参数个数:" + method.getParameterCount());
                System.out.println("参数数组:" + Arrays.toString(method.getParameters()));
            }
            // 创建一个Student对象
            Student stu = new Student();
            // 单独获取一个方法【方法名称必须和类中名完全匹配】
            Method method = clazz.getDeclaredMethod("setAge", int.class, String.class);
            // 开启任何方法的访问权限
            method.setAccessible(true);
            Object object = method.invoke(stu, 10, "张三");
            System.out.println(stu.getName());
    
            /*
             * Field field = clazz.getDeclaredField("age"); // 开启访问权限
             * field.setAccessible(true); Object value = field.get(stu);
             * System.out.println(value);
             */
    
            Method getMethod = clazz.getDeclaredMethod("getAge");
            getMethod.setAccessible(true);
            Object value = getMethod.invoke(stu);
            System.out.println(value);
        }

    四.通过Class对象调用对应类的构造函数创建对象的实例

      @Test 
        public void testClassForConstructor() throws NoSuchMethodException, Exception {
            System.out.println("------- 通过Class对象调用对应类的构造函数创建对象的实例 --------");
            Class clazz = Student.class;
            // 获取使用public修饰的构造器
            Constructor[] constructors = clazz.getConstructors();
            for (Constructor cl : constructors) {
                System.out.println(cl);
            }
            System.out.println("--------------------------");
            // 获取当前类中所有的构造器
            constructors = clazz.getDeclaredConstructors();
            for (Constructor cl : constructors) {
                System.out.println(cl);
                System.out.println("访问修饰符:"+Modifier.toString(cl.getModifiers()));
                System.out.println("构造器名称:"+cl.getName());
                System.out.println("参数类型数组:"+Arrays.toString(cl.getParameterTypes()));
            }
            
            // 获取一个构造器对象
            Constructor conl = clazz.getDeclaredConstructor(int.class, String.class);
            // 开启任何构造函数的访问权限
            conl.setAccessible(true);
            // 通过参数个数和类型创建对象的实例
            Object object = conl.newInstance(100, "AAA");
            System.out.println(object);
        }

     Q:为什么类必须得有一个无参构造函数?

     A:在使用反射实例化类时会调用类的无参构造函数。 Object instance = clazz.newInstance(); 

  • 相关阅读:
    Azure PowerShell (7) 使用CSV文件批量设置Virtual Machine Endpoint
    Windows Azure Cloud Service (39) 如何将现有Web应用迁移到Azure PaaS平台
    Azure China (7) 使用WebMetrix将Web Site发布至Azure China
    Microsoft Azure News(4) Azure新D系列虚拟机上线
    Windows Azure Cloud Service (38) 微软IaaS与PaaS比较
    Windows Azure Cloud Service (37) 浅谈Cloud Service
    Azure PowerShell (6) 设置单个Virtual Machine Endpoint
    Azure PowerShell (5) 使用Azure PowerShell创建简单的Azure虚拟机和Linux虚拟机
    功能代码(1)---通过Jquery来处理复选框
    案例1.用Ajax实现用户名的校验
  • 原文地址:https://www.cnblogs.com/lql6/p/10083290.html
Copyright © 2011-2022 走看看