√ JavaSE - 26.怎么使用反射(卷1 P198)

  1. Class类是一个泛型类,Class类的对象表示一个运行时类型。
  2. 加载到内存中的类,会缓存一定的时间,在此期间内可以通过不同的方式获取这个类的信息。
  3. 类的加载过程:
    1. 加载。将类的class文件读入内存,并由类加载器为之创建一个java.lang.Class对象。
    2. 链接。将类的二进制数据合并到jre中。
    3. 初始化。jvm负责类的初始化。
  4. 利用反射实例化类的对象通常是通过调用类的空参构造器实现的,因此需要保证类具有空参构造器,且是public的。
import java.lang.annotation.Annotation;
import java.lang.reflect.*;

public class ReflectionTest {
    public static void main(String[] args) throws Exception {
        //获取类的信息
        Class personClass = Class.forName("Person");

        //获取指定的public构造器、字段、方法信息
        Constructor constructor = personClass.getConstructor(String.class);
        Person person1 = (Person) constructor.newInstance("Tom");
        System.out.println(person1);

        Field name = personClass.getField("name");
        name.set(person1, "Tim");
        System.out.println(person1);

        Method setName = personClass.getMethod("setName", String.class);
        setName.invoke(person1, "Tem");
        System.out.println(person1);
        System.out.println();

        //获取指定的private构造器、字段、方法信息
        Constructor declaredConstructor = personClass.getDeclaredConstructor(String.class, int.class);
        declaredConstructor.setAccessible(true);
        Person person2 = (Person) declaredConstructor.newInstance("Jerry", 18);
        System.out.println(person2);

        Field age = personClass.getDeclaredField("age");
        age.setAccessible(true);
        age.set(person2, 24);
        System.out.println(person2);

        Method setAge = personClass.getDeclaredMethod("setAge", int.class);
        setAge.setAccessible(true);
        setAge.invoke(person2, 35);
        System.out.println(person2);
        System.out.println();

        //获取所有的方法信息
        Method[] declaredMethods = personClass.getDeclaredMethods();
        for (Method method : declaredMethods) {
            //注解
            Annotation[] annotations = method.getDeclaredAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println("注解:" + annotation);
            }
            //权限修饰符
            System.out.println("权限修饰符:" + Modifier.toString(method.getModifiers()));
            //返回值类型
            System.out.println("返回值类型:" + method.getReturnType().getName());
            //方法名
            System.out.println("方法名:" + method.getName());
            //参数
            Parameter[] parameters = method.getParameters();
            for (Parameter parameter : parameters) {
                System.out.println("参数:" + parameter);
            }
            //异常
            Class[] exceptionTypes = method.getExceptionTypes();
            for (Class exceptionType : exceptionTypes) {
                System.out.println("异常:" + exceptionType.getName());
            }
            System.out.println();
        }
    }
}

@interface MyAnnotation {
    String value() default "Annotation";
}

@MyAnnotation()
class Person {
    @MyAnnotation(value = "publicField")
    public String name;
    @MyAnnotation(value = "privateField")
    private int age;

    @MyAnnotation(value = "publicConstructor")
    public Person(String name) {
        this.name = name;
    }

    @MyAnnotation(value = "privateConstructor")
    private Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @MyAnnotation(value = "publicMethod")
    public void setName(String name) throws RuntimeException {
        this.name = name;
    }

    @MyAnnotation(value = "privateMethod")
    private void setAge(int age) throws IndexOutOfBoundsException, IllegalArgumentException {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值