Java - 通过反射进行赋值以及函数调用
创始人
2024-03-24 11:39:01
0

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

  • 前言
  • 一. 通过反射进行赋值
    • 1.1 测试
    • 1.2 总结
  • 二. 通过反射进行函数调用

前言

说来惭愧,虽然反射在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);// 因为是私有的,所以要想访问,需要设置Accessiblehello.setAccessible(true);// 对应的参数Object[] objects = {"LJJ", 24};// 调用函数hello.invoke(teacher, objects);
}

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

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...