Java 反射 从基本到深入

Java 反射机制


基本反射

/**
 * getClass 方法
 */
 String name = "bi wen bo";
 Class c = name.getClass();
 System.out.println("name.getClass(): " + name.getClass() + "  -  " + "c.getName(): " + c.getName());

/**
 * Class.forName
 */
 String sub = "com.invoke.RealSubject";
 Class c2 = null;
    try {
        c2 = Class.forName(sub);
        System.out.println("c2.getName(): " + c2.getName() + " c2 父类: " + c2.getSuperclass());
     } catch (ClassNotFoundException e) {
        e.printStackTrace();
     }

结果:
在这里插入图片描述
若传的字符串命名不合法,抛出异常:
在这里插入图片描述
获取基本类型

/**
 * TYPE
 */
System.out.println("Integer: " + Integer.TYPE + " Byte: " + Byte.TYPE + " Boolean: " + Boolean.TYPE + " Float: " + Float.TYPE);

结果:
在这里插入图片描述
获取类的成员

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

/**
 * java 反射
 *
 * @author bobo
 * @since 2020/8/11 11:33
 */
public class Reflection {

    private String name;
    private String sex;
    private int phone;
    private int password;

    public Reflection () {}

    public Reflection (String name) {
        this.name = name;
    }

    private Reflection (String name, String sex, int phone) {
        this.name = name;
        this.sex = sex;
        this.phone = phone;
    }

    public Reflection (String name, String sex, int phone, int password) {
        this.name = name;
        this.sex = sex;
        this.phone = phone;
        this.password = password;
    }

    public static void main(String[] args) {
        Reflection reflect = new Reflection();
        Class ref = reflect.getClass();
        Constructor[] constructors = ref.getDeclaredConstructors();
        for (int i = 0; i < constructors.length; i ++) {
            System.out.println(Modifier.toString(constructors[i].getModifiers()) + "参数: ");
            Class[] types = constructors[i].getParameterTypes();
            for (int j = 0; j < types.length; j ++) {
                System.out.println(types[j].getName() + " ");
            }
            System.out.println("");
        }
    }
}

结果:
在这里插入图片描述
获取指定的构造方法

try {
     Constructor singleConstructor = ref.getDeclaredConstructor();
     System.out.println("对应构造方法类型: " + Modifier.toString(singleConstructor.getModifiers()) + " ");
 } catch (NoSuchMethodException e) {
     e.printStackTrace();
 }

调用构造方法
打印构造方法:

private Reflection (String name, String sex, int phone) {
    this.name = name;
    this.sex = sex;
    this.phone = phone;
    System.out.println("name: " + name + " sex: " + sex + " phone : " + phone);
}
/**
 * 调用构造方法
 */
Class[] p = {String.class, String.class, int.class};
Constructor newConstructors = ref.getDeclaredConstructor(p);
newConstructors.newInstance("bwb", "girl", 123456);

结果:
在这里插入图片描述
== 调用私有构造方法时,要设置constructors.setAccessible(true);==

调用私有构造方法:

/**
 * 调用私有构造方法
 */
   Class[] p1 = {String.class};
   newConstructors = ref.getDeclaredConstructor(p1);
   newConstructors.setAccessible(true);
   newConstructors.newInstance("hei");

类的私有方法:

/**
 * 私有方法
 *
 * @param tips
 */
    private void welcome (String tips) {
        System.out.println(tips);
    }

调用类的私有方法:

/**
 * 调用类的私有方法
 * 首先通过 getDeclaredMethod方法获取到这个私有方法,第一个参数是方法名,第二个参数是参数类型
 * 然后通过invoke方法执行,invoke需要两个参数一个是类的实例,一个是方法参数
 */
    Class[] newp = {String.class};
    Method method = ref.getDeclaredMethod("welcome", newp);
    method.setAccessible(true);
    Object arg[] = {"欢迎"};
    method.invoke(reflect, arg);

结果:
在这里插入图片描述
获取类的私有字段并修改值

    Reflection reflect = new Reflection();
    Class ref = reflect.getClass();
/**
 * 获取类的私有字段并修改值
 */
    Field field = ref.getDeclaredField("name");
    field.setAccessible(true);
    field.set(reflect, "okk");
    System.out.println(field.get(reflect).toString());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值