1. 第一组:java.lang.Class类
package com.model.reflection;

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

/**
 * @Description:测试类
 * @Author: 张紫韩
 * @Crete 2021/6/29 15:24
 * 演示获取类的结构信息
 */
public class ReflectionDemo04 {
    /**
     * 第一组API:
     * 1. getName:获取全类名
     * 2. getSimpleName:获取简单类名
     * 3. getFields:获取所有public修饰的属性,包含本类以及父类的
     * 4. getDeclaredFields:获取本类中所有属性
     * 5. getMethods:获取所有public修饰的方法,包含本类以及父类的
     * 6. getDeclaredMethods:获取本类中所有方法
     * 7. getConstructors:获取所有public修饰的构造器,包含本类
     * 8. getDeclaredConstructors:获取本类中所有构造器
     * 9. getPackage:以Package形式返回包信息
     * 10.getSuperClass:以Class形式返回父类信息
     * 11.getlnterfaces:以Class[1形式返回接口信息
     * 12.getAnnotations:以Annotation[]形式返回注解信息
     * */

    public static void main(String[] args) throws ClassNotFoundException {
//        1.获取Class类对象
        Class<?> aClass = Class.forName("com.model.reflection.Person");
//                * 1. getName:获取全类名
        System.out.println(aClass.getName());
//                * 2. getSimpleName:获取简单类名
        System.out.println(aClass.getSimpleName());
//                * 3. getFields:获取所有public修饰的属性,包含本类以及父类的
        Field[] fields = aClass.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
//                * 4. getDeclaredFields:获取本类中所有属性
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field field : declaredFields) {
            System.out.println(field);
        }
//                * 5. getMethods:获取所有public修饰的方法,包含本类以及父类的
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
//                * 6. getDeclaredMethods:获取本类中所有方法
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
//                * 7. getConstructors:获取所有public修饰的构造器,包含本类
        Constructor<?>[] constructors = aClass.getConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }
//                * 8. getDeclaredConstructors:获取本类中所有构造器
        Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
//                * 9. getPackage:以Package形式返回包信息
        Package aPackage = aClass.getPackage();
        System.out.println(aPackage);
//                * 10.getSuperClass:以Class形式返回父类信息
        System.out.println(aClass.getSuperclass());
//                * 11.getInterfaces:以Class[1]形式返回接口信息
        Class<?>[] interfaces = aClass.getInterfaces();
        for (Class<?> anInterface : interfaces) {
            System.out.println(anInterface);
        }
//                * 12.getAnnotations:以Annotation[]形式返回注解信息
        Annotation[] annotations = aClass.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }


    }
}
class Person{
    public String name;
    protected int age;
    private double sal;
    public void m1(){
        System.out.println("m1");
    };
    protected void m2(){
        System.out.println("m2");
    };
    private void m3(){
        System.out.println("m3");
    };

}
  1. 第二组 java.lang.reflect.Field类
  2. 第三组
  3. 第四组:
package com.model.reflection;


import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;

/**
 * @Description:测试类
 * @Author: 张紫韩
 * @Crete 2021/6/29 15:24
 * 演示获取类的结构信息
 */
public class ReflectionDemo04 {
    /**
     * 第一组API:
     * 1. getName:获取全类名
     * 2. getSimpleName:获取简单类名
     * 3. getFields:获取所有public修饰的属性,包含本类以及父类的
     * 4. getDeclaredFields:获取本类中所有属性
     * 5. getMethods:获取所有public修饰的方法,包含本类以及父类的
     * 6. getDeclaredMethods:获取本类中所有方法
     * 7. getConstructors:获取所有public修饰的构造器
     * 8. getDeclaredConstructors:获取本类中所有构造器
     * 9. getPackage:以Package形式返回包信息
     * 10.getSuperClass:以Class形式返回父类信息
     * 11.getInterfaces:以Class[1形式返回接口信息
     * 12.getAnnotations:以Annotation[]形式返回注解信息
     * */

//    1.API调用
    @Test
    public void test_API() throws ClassNotFoundException {
//        1.获取Class类对象
        Class<?> aClass = Class.forName("com.model.reflection.Person");
//                * 1. getName:获取全类名
        System.out.println(aClass.getName());
//                * 2. getSimpleName:获取简单类名
        System.out.println(aClass.getSimpleName());
//                * 3. getFields:获取所有public修饰的属性,包含本类以及父类的
        Field[] fields = aClass.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
//                * 4. getDeclaredFields:获取本类中所有属性
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field field : declaredFields) {
            System.out.println(field.getName());
        }
//                * 5. getMethods:获取所有public修饰的方法,包含本类以及父类的
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
//                * 6. getDeclaredMethods:获取本类中所有方法
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
//                * 7. getConstructors:获取所有public修饰的构造器
        Constructor<?>[] constructors = aClass.getConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }
//                * 8. getDeclaredConstructors:获取本类中所有构造器
        Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
//                * 9. getPackage:以Package形式返回包信息
        Package aPackage = aClass.getPackage();
        System.out.println(aPackage);
//                * 10.getSuperClass:以Class形式返回父类信息
        System.out.println(aClass.getSuperclass());
//                * 11.getInterfaces:以Class[1]形式返回接口信息
        Class<?>[] interfaces = aClass.getInterfaces();
        for (Class<?> anInterface : interfaces) {
            System.out.println(anInterface);
        }
//                * 12.getAnnotations:以Annotation[]形式返回注解信息
        Annotation[] annotations = aClass.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }




    }

//    2.成员变量Field属性
    @Test
    public void  test_Field() throws ClassNotFoundException {

        Class<?> aClass = Class.forName("com.model.reflection.Person");
        Field[] fields = aClass.getFields(); //获取本类中(包括父类)所有的public修饰的成员变量
        for (Field field : fields) {
            System.out.println(field.getName()); //成员变量名
            //成员变量的访问修饰符的值,
            //说明:默认修饰符是0,public 是1, private是2,protected是4,static是8, final是16
            System.out.println(field.getModifiers());
        }
        System.out.println("****************************");
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println("成员变量的名字"+declaredField.getName()); //获取本类中所有的成员变量
            //获取本类中成员变量的访问属性,
            //有两个修饰符修饰 ,则修饰符的值相加
            System.out.println("成员变量的访问修饰的值"+declaredField.getModifiers());
            System.out.println("成员变量的类型"+declaredField.getType());
        }


    }
//    3.成员方法Method属性
    @Test
    public void test_Method() throws ClassNotFoundException {

        Class<?> aClass = Class.forName("com.model.reflection.Person");
        Method[] methods = aClass.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println("成员方法的名字"+method.getName());
            System.out.println("成员方法的修饰符的值"+method.getModifiers());
            System.out.println("成员方法的返回值类型"+method.getReturnType());

            //成员方法的参数
            TypeVariable<? extends Class<?>>[] typeParameters = aClass.getTypeParameters();
            for (TypeVariable<? extends Class<?>> typeParameter : typeParameters) {
                System.out.println(typeParameter);
            }
        }
    }

//    4.构造器Constructor属性
    @Test
    public void test_Constructor() throws ClassNotFoundException {
        Class<?> aClass = Class.forName("com.model.reflection.Person");
        Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {

            System.out.println("构造器的全类名:"+declaredConstructor.getName());
            System.out.println("构造器的修饰符的值:"+declaredConstructor.getModifiers());
            Class<?>[] parameterTypes = declaredConstructor.getParameterTypes();
            for (Class<?> parameterType : parameterTypes) {
                System.out.println("构造器的参数:"+parameterType.getName());
            }


        }
    }


}


class Person{
    public Person() {
    }

    public Person(String name, int age, double sal) {
        this.name = name;
        this.age = age;
        this.sal = sal;
    }

    public String name;
    protected int age;
    private double sal;
    public void m1(){
        System.out.println("m1");
    };
    protected void m2(){
        System.out.println("m2");
    };
    private void m3(){
        System.out.println("m3");
    };

}