Java - 通过反射进行赋值以及函数调用

前言

说来惭愧,虽然反射在Java中是非常重要和常见的一种机制。但是,每当自己去写这方面的代码的时候,总是容易愣住。还得想一想代码怎么写。因此写下这篇文章做个笔记。

可以先看下这篇文章 Java-通过反射来打印类

一. 通过反射进行赋值

1.我们准备一个Teacher类,并把相关的属性都设置为private私有。准备他的get函数,set就不必啦。

public class Teacher {
    private int userId;
    private String userName;

    public int getUserId() {
        return userId;
    }

    public String getUserName() {
        return userName;
    }

    private void hello(String name, Integer userId) {
        System.out.println("Hello World," + name + ", " + userId);
    }
}

那么我们如何通过反射来进行赋值呢?关键代码:拿到这个类中的字段。

Field fieldName= xxx.class.getDeclaredField("fieldName");

1.1 测试

测试1:

@org.junit.Test
public void test3() throws Exception {
    Teacher teacher = new Teacher();
    Field userName = Teacher.class.getDeclaredField("userName");
    // 授权访问私有成员变量
    userName.setAccessible(true);
    userName.set(teacher, "ljj");
    System.out.println(teacher.getUserName());:
    System.out.println(teacher.getUserId());
}

结果如下:可见userId输出为0。因为我们没有对它进行赋值。所以初始值为0,而userName则赋值成功。
在这里插入图片描述
测试2:setAccessible我们设置为false(默认)

@org.junit.Test
public void test3() throws Exception {
    Teacher teacher = new Teacher();
    System.out.println(teacher.getUserId());
    Field userId = Teacher.class.getDeclaredField("userId");
    userId.setAccessible(false);
    userId.set(teacher, 10);
    System.out.println(teacher.getUserId());
}

结果如下:
在这里插入图片描述
测试3:setAccessible设置为true,然后再次赋值userId

@org.junit.Test
public void test3() throws Exception {
    Teacher teacher = new Teacher();
    System.out.println(teacher.getUserId());
    Field userId = Teacher.class.getDeclaredField("userId");
    userId.setAccessible(true);
    userId.set(teacher, 10);
    System.out.println(teacher.getUserId());
}

结果如下:
在这里插入图片描述
测试4:给Teacher加一个public类型的属性address

public String address;
public String getAddress() {
    return address;
}

测试如下:

@org.junit.Test
public void test3() throws Exception {
    Teacher teacher = new Teacher();
    Field address = Teacher.class.getDeclaredField("address");
    address.set(teacher, "abcd");
    System.out.println(teacher.getAddress());
}

结果:
在这里插入图片描述

1.2 总结

  1. 可以通过Field name= Object.class.getDeclaredField("name");的方式拿到类的字段。
  2. 通过name.set(Target, value);进行属性的赋值。
  3. 如果字段是私有的,还需要设置setAccessible(true); 其他不需要。

二. 通过反射进行函数调用

关键代码:

Method method = Teacher.class.getDeclaredMethod(方法名称, 参数类型1.class, 参数类型2.class,...);

以本文案例为例:

private void hello(String name, Integer userId) {
    System.out.println("Hello World," + name + ", " + userId);
}

那么不难写出代码如下:

@org.junit.Test
public void test3() throws Exception {
    Teacher teacher = new Teacher();
    // 写对方法名称、参数类型
    Method hello = Teacher.class.getDeclaredMethod("hello", String.class, Integer.class);
    // 因为是私有的,所以要想访问,需要设置Accessible
    hello.setAccessible(true);
    // 对应的参数
    Object[] objects = {"LJJ", 24};
    // 调用函数
    hello.invoke(teacher, objects);
}

结果如下:
在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zong_0915

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值