【转载】Java反射

参考

  1. B站狂神说
  2. https://www.cnblogs.com/ysocean/p/6516248.html

提示

  • 反射比直接new要慢,但是能解耦,流行框架用的很多,注解和反射在一起用。
  • 反射也可以获取父类的信息

代码

  1. User 被反射的类代码
package src;
public class User {

    static {
        System.out.println("对象"+User.class.getName()+"被JVM初始化了");
    }

    private Integer id;
    private String name;
    private String phone;
    private Byte age;
    private String address;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Byte getAge() {
        return age;
    }

    public void setAge(Byte age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }
}
  1. Main 测试代码
package src;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, IOException, NoSuchFieldException {
        // 加载指定类
        Class userClass  = Class.forName("src.User");
        // 反射实例化
        User user = (User) userClass.getDeclaredConstructor().newInstance();
        // 获取类中的所有公共字段
        Field[] fields = user.getClass().getFields();
        for (Field field : fields) {
            System.out.println("公共属性"+field.getName());
        }
        // 获取类的所有字段
        fields = user.getClass().getDeclaredFields();
        for (Field field : fields) {
            System.out.println("所有属性"+field.getName());
        }

        // 获取公共属性
//        System.out.println(user.getClass().getField("name"));
        // 获取私有属性
        Field field = user.getClass().getDeclaredField("name");
        // 关闭安全检查
        field.setAccessible(true);
        // 通过获取的属性给指定对象执行赋值
        field.set(user, "李四");

        // 获取类中所有公共方法
        Method[] methods = user.getClass().getMethods();
        for (Method method : methods) {
            System.out.println("公共方法"+method.getName());
        }
        // 获取类中所有方法(包括Object的方法)
        methods = user.getClass().getDeclaredMethods();
        for (Method method : methods) {
            System.out.println("公共方法"+method.getName());
        }

        // 获取指定公共方法
        Method method = user.getClass().getMethod("setName", String.class);
        // 获取指定方法
        method = user.getClass().getDeclaredMethod("setName", String.class);
        // 获取返回类型
        method.getReturnType();
        // 获取参数类型
        method.getParameterTypes();
        // 关闭安全检查
        method.setAccessible(true);
        // 执行指定方法 给指定类
        System.out.println(user.toString());
        // 通过获取的方法 invoke激活然后给指定方法执行指定方法,携带参数
        method.invoke(user, "张三");
        //
        System.out.println(user.toString());
    }
}

posted @ 2022-02-16 14:27  夏秋初  阅读(28)  评论(0)    收藏  举报