反射知识总结

反射

1、概念

  • 反射是指在运行时,获取类的信息,创建对象,获取属性,调用方法的一个过程。

2、获取Class类的三种方式

2.1、通过类获取

  • 方式是:Student.class获取类的字节码对象。
  • 代码
@Test
public void test01(){
  Class<Student> clss = Student.class;
}

2.2、通过对象获取

  • 方式是:对象名.getClass()方法。
  • 代码
@Test
public void test02(){
  Student student = new Student();
  Class<? extends Student> clss = student.getClass();
}

2.3、通过Class静态方法获取

  • 方式是Class.forName(…)
  • 代码
@Test
public void test03() throws ClassNotFoundException {
  Class<?> clss = Class.forName("com.yan.entity.Student");
}

3、获取构造方法

  • 获取所有公共构造方法
  • 获取所有构造方法,包括私有
  • 获取制定参数类型的构造方法
@Test
public void test03() throws Exception {
    Class<?> clss = Class.forName("com.yan.entity.Student");
    // 获取所有公共的构造方法
    Constructor<?>[] constructors = clss.getConstructors();
    // 获取所有的构造方法
    Constructor<?>[] declaredConstructors = clss.getDeclaredConstructors();
    // 获取制定参数类型的构造方法
    Constructor<?> constructor = clss.getDeclaredConstructor(Integer.class, String.class, String.class);
}

4、反射创建对象的过程

4.1、过程

  • 第一步,创建字节码对象
  • 第二步,获取构造方法
  • 第三步,构造方法设置setAccessible(true)
  • 第四步,通过newInstance(…)传值创建对象。
  • 注意实体类的字段类型使用使用包装类型。

4.2、代码

  • 代码1
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {

    private Integer id;
    private String name;

}
  • 代码2
@Test
public void test04() throws Exception {
    Class<?> clss = Class.forName("com.yan.entity.Student");
    // 获取制定参数类型的构造方法
    Constructor<?> constructor = clss.getDeclaredConstructor(Integer.class, String.class);
    constructor.setAccessible(true);
    Object obj = constructor.newInstance(1, "zhangsan");
    System.out.println(obj);
}

5、获取字段

5.1、获取字段方式

  • 获取所有公共字段
  • 获取所有字段
  • 获取指定字段
@Test
public void test05() throws Exception {
    Class<?> clss = Class.forName("com.yan.entity.Student");
    // 获取所有公共字段
    Field[] fields = clss.getFields();
    // 获取所有字段,包括私有
    Field[] declaredFields = clss.getDeclaredFields();
    // 获取制定字段
    Field name = clss.getDeclaredField("name");
}

5.2、给字段赋值

  • 获取字节码对象
@Test
public void test06() throws Exception {
    Class<?> clss = Class.forName("com.yan.entity.Student");
    // 获取构造方法
    Constructor<?> constructor = clss.getDeclaredConstructor();
    constructor.setAccessible(true);
    Object obj = constructor.newInstance();

    // 获取字段
    Field nameFile = clss.getDeclaredField("name");
    nameFile.setAccessible(true);
    // 给对象赋值
    nameFile.set(obj,"zhangsan");
    // 取值
    System.out.println(nameFile.get(obj));
}

6、获取方法

6.1、获取方法的方式

  • 获取所有公共方法
  • 获取所有方法,包括私有,不包括继承。
  • 获取指定方法
@Test
public void test07() throws Exception {
  Class<?> clss = Class.forName("com.yan.entity.Student");
  // 获取所有公共方法
  Method[] methods = clss.getMethods();
  // 获取所有方法,包括私有方法,不包括继承的方法
  Method[] declaredMethods = clss.getDeclaredMethods();
  // 获取指定方法
  Method getName = clss.getDeclaredMethod("getName");
}

6.2、调用方法

  • 代码1
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {

    private Integer id;
    private String name;

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

    public Student(Integer id) {
        this.id = id;
    }

    public void show(String something) {
        System.out.println("调用show方法,执行" + something);
    }
}
  • 代码2
@Test
public void test08() throws Exception {
    Class<?> clss = Class.forName("com.yan.entity.Student");
    Constructor<?> constructor = clss.getDeclaredConstructor(Integer.class, String.class);
    constructor.setAccessible(true);
    Object obj = constructor.newInstance(1, "zhangsan");

    Method showMethod = clss.getDeclaredMethod("show", String.class);
    showMethod.setAccessible(true);
    showMethod.invoke(obj,"看电视指令");
}
  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值