注解

什么是注解

内置注解

元注解

自定义注解

反射

反射概述

image-20200524195147981

获得反射对象

image-20200524195430683

image-20200909134501116

image-20200909134600427

得到class类的几种方式

image-20200909140451559

image-20200909140554881

代码演示

Test01

package com.zhiyu.reflection;

public class Test01 {
    public static void main(String[] args) throws ClassNotFoundException {
        //通过反射获取类的Class对象
        Class<?> c1 = Class.forName("com.zhiyu.reflection.User");
        System.out.println(c1);

        //一个类在内存中只有一个Class对象
        //一个类被加载后,整个类的结构都会封装在一个Class对象中
        Class<?> c2 = Class.forName("com.zhiyu.reflection.User");
        Class<?> c3 = Class.forName("com.zhiyu.reflection.User");
        System.out.println(c1.hashCode() + "\n" + c2.hashCode() + "\n" + c3.hashCode() );

        //通过对象获得
        User user = new User();
        Class<? extends User> c4 = user.getClass();

        //通过类名.class获得
        Class<User> c5 = User.class;
        System.out.println(c4.hashCode() + "\n" + c5.hashCode());


        //获取父类
        Class<? super User> superclass = c5.getSuperclass();
        System.out.println(superclass);
    }
}

class User{
    private int id;
    private String name;

    public User() {
    }

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Test02

package com.zhiyu.reflection;

import java.lang.annotation.ElementType;

public class Test02 {
    public static void main(String[] args) {
        Class c1 = Object.class;
        Class c2 = Comparable.class;
        Class c3 = String[].class;
        Class c4 = int[][].class;
        Class c5 = Override.class;
        Class c6 = ElementType.class;
        Class c7 = Integer.class;
        Class c8 = void.class;
        Class c9 = Class.class;

        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);
        System.out.println(c4);
        System.out.println(c5);
        System.out.println(c6);
        System.out.println(c7);
        System.out.println(c8);
        System.out.println(c9);
    }
}

Test03

package com.zhiyu.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test03 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class<?> c1 = Class.forName("com.zhiyu.reflection.User");

        //获取类的名字
        System.out.println(c1.getName());
        System.out.println(c1.getSimpleName());

        //获取类的全部属性(public)
        Field[] fields = c1.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        System.out.println("************************");
        //获取全部属性
        Field[] declaredFields = c1.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }

        //获取指定的属性
//        Field name = c1.getField("name");
        Field name1 = c1.getDeclaredField("name");
        System.out.println(name1);

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

        //获取类的方法,本类及其父类的所有方法
        Method[] methods = c1.getMethods();
        Method[] declaredMethods = c1.getDeclaredMethods();

        //获得指定的方法
        Method getName = c1.getMethod("getName",null);
        Method setName = c1.getMethod("setName", String.class);

        //获取构造器
        Constructor<?>[] constructors = c1.getConstructors();
        Constructor<?>[] declaredConstructors = c1.getDeclaredConstructors();

        //获得指定的构造器
        Constructor<?> declaredConstructor = c1.getDeclaredConstructor(int.class, String.class);

    }
}

Test04

package com.zhiyu.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test04 {
    //通过反射动态创建对象
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        Class<?> c1 = Class.forName("com.zhiyu.reflection.User");

        //构造对象
        User user = (User) c1.newInstance();//本质是调用了类的无参构造,如果此类没有无参构造就会报错

        //如果没有无参构造可以通过Class对象获取有参构造来创建对象
        Constructor<?> constructor = c1.getDeclaredConstructor(int.class, String.class);
        User user2 = (User) constructor.newInstance(1, "小明");
        System.out.println(user2.getId() + user2.getName());

        //反射去调用方法
        User user3 = (User) c1.newInstance();
        Method setName = c1.getDeclaredMethod("setName", String.class);
        //激活:调用哪个对象里的setName方法,方法的参数
        setName.invoke(user3,"小华");
        System.out.println(user3.getName());

        //反射操作属性
        User user4 = (User) c1.newInstance();
        Field name = c1.getDeclaredField("name");
        //不能直接操作,需要关掉权限检测
        name.setAccessible(true);
        name.set(user4,"小红");
        System.out.println(user4.getName());
    }
}

Q.E.D.

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

ALL WILL BE CLEAR