目录

 反射获取类对象四种方式

反射获取类名、属性、方法

反射创建对象

反射设置字段值及方法返回值

反射获取注解


 反射获取类对象四种方式

  1. 通过Class.forName(“类的包名全路径”)

    Class<?> aClass = Class.forName("cht.reflect.ReflectTest02");
  2. 类名.class拿到反射对象

    Class<Person> reflectTest02Class = Person.class;
  3. 类的getClass

     Person person = new Person();
            Class<? extends Person> aClass1 = person.getClass();
  4. 包装类的类型

    Class<Integer> type = Integer.TYPE;

    以下三种方式不会初始化子类:

    // 1.子类直接调用父类的静态变量不会调用clinit初始化加载子类
    System.out.println(Student.age);
    
    // 2.类的数组不会调用clinit初始化加载子类
    Student[] arr = new Student[2];
    
    // 3.直接输出常量。也不会调用clinit初始化加载子类
    System.out.println(Student.id);
    

反射获取类名、属性、方法

  1. 获取类名

            // 全路径类名
            String name = aClass.getName();
            // 类名
            String simpleName = aClass.getSimpleName();
  2. 获取公共方法/私有方法

    // 获取本类及父类的所有方法(不包含私有方法)
    Method[] methods = aClass.getMethods();
    // 获取本类所有方法及私有方法
    Method[] declaredMethods = aClass.getDeclaredMethods();
  3. 获取公共字段/私有字段

    // 获取所有非私有字段
    Field[] fields = aClass.getFields();
    // 获取所有字段包含私有字段
    Field[] declaredFields = aClass.getDeclaredFields();

反射创建对象(两种方式)

            // 1.通过newInstance创建对象 jdk9之后不建议使用
            Class<?> aClass = Class.forName("cht.test.Animal");
            Object object = aClass.newInstance();
            System.out.println(object);
            System.out.println("==========================");

            // 2.通过反射获取构造方法创建对象
            Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class);
            Animal cat = (Animal) declaredConstructor.newInstance("cat");
            System.out.println(cat);
            System.out.println(cat.getName());
            System.out.println("==========================");

反射设置字段值及方法返回值

            // 1.设置字段的值
            Field name = aClass.getDeclaredField("name");
            name.setAccessible(true);
            name.set(cat,"dog");
            System.out.println(cat);
            System.out.println(cat.getName());
            System.out.println("==========================");

            // 2.设置set()方法的值
            Method declaredMethod = aClass.getDeclaredMethod("setName", String.class);
            declaredMethod.invoke(cat,"fish");
            System.out.println(cat);
            System.out.println(cat.getName());

反射获取注解

{
        Class<?> testPet = Class.forName("cht.reflect.TestPet");
        // 获取类的所有注解
        Annotation[] annotations = testPet.getAnnotations();
        for (Annotation annotation : annotations) {
            // 获取注解类型
            Class<? extends Annotation> annotationType = annotation.annotationType();
            // 获取自定义注解的所有注解
            Annotation[] annotations1 = annotationType.getAnnotations();
            for (Annotation annotation1 : annotations1) {
                System.out.println(annotation1);
            }
        }

        System.out.println("==============================================");

        // 获取字段的注解
        Field[] fields = testPet.getDeclaredFields();
        for (Field field : fields) {
            Annotation[] declaredAnnotations = field.getDeclaredAnnotations();
            for (Annotation declaredAnnotation : declaredAnnotations) {
                System.out.println(declaredAnnotation);
            }
        }

        System.out.println("==============================================");

        // 获取类注解的值
        annotation1 annotation = testPet.getAnnotation(annotation1.class);
        System.out.println(annotation.value());
        System.out.println("==============================================");
        // 获取类字段注解的值
        Field name = testPet.getDeclaredField("Name");
        annotation2 declaredAnnotation = name.getDeclaredAnnotation(annotation2.class);
        int length = declaredAnnotation.length();
        String type = declaredAnnotation.type();
        System.out.println(type + "|" + length);

    }
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐