### Java反射创建get set方法及反射方法的调用 #### 概述 在Java编程语言中,反射(Reflection)是一种强大的技术,它允许程序在运行时检查类、接口、字段和方法的信息,并能够动态地创建对象和调用方法。本文将详细介绍如何利用Java反射来创建get和set方法,并演示如何通过反射机制调用这些方法。 #### Java反射概述 Java反射机制提供了一种方式,可以在运行时获取类的信息,并创建和操作对象。这为开发者提供了极大的灵活性,但也可能导致代码难以理解和维护。反射通常用于框架开发、动态代理等方面。 #### 创建get set方法 在给定的代码示例中,我们看到一个名为`PropertyUtil`的工具类,该类提供了三个主要的方法:`getPropertyDescriptor`、`setProperty` 和 `getProperty`。 1. **getPropertyDescriptor** 方法: - 这个方法的作用是根据传入的类`clazz`和属性名`propertyName`,返回一个`PropertyDescriptor`对象。 - 首先尝试获取类中的字段,如果找不到对应的字段,则构建`get`和`set`方法名。 - 使用反射机制查找对应的`set`和`get`方法,并创建一个`PropertyDescriptor`对象。 2. **setProperty** 方法: - 该方法接收一个对象`obj`、属性名`propertyName`和要设置的值`value`。 - 首先通过`getPropertyDescriptor`获取到对应的`PropertyDescriptor`。 - 调用`PropertyDescriptor`的`getWriteMethod()`获取写方法(即set方法),并调用该方法更新对象属性的值。 3. **getProperty** 方法: - 该方法接收一个对象`obj`和属性名`propertyName`。 - 通过`getPropertyDescriptor`获取对应的`PropertyDescriptor`。 - 调用`PropertyDescriptor`的`getReadMethod()`获取读方法(即get方法),并调用该方法获取对象属性的值。 #### 示例代码解析 1. **获取PropertyDescriptor**: ```java public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName) { // 尝试获取类中的字段 Field f = clazz.getDeclaredField(propertyName); if (f == null) { // 构建get和set方法名 String methodEnd = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method setMethod = clazz.getDeclaredMethod("set" + methodEnd, new Class[]{f.getType()}); Method getMethod = clazz.getDeclaredMethod("get" + methodEnd, new Class[]{}); // 创建PropertyDescriptor return new PropertyDescriptor(propertyName, getMethod, setMethod); } return null; } ``` 2. **设置属性值**: ```java public static void setProperty(Object obj, String propertyName, Object value) { // 获取类 Class clazz = obj.getClass(); // 获取PropertyDescriptor PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName); // 获取写方法(set方法) Method setMethod = pd.getWriteMethod(); // 设置属性值 setMethod.invoke(obj, new Object[]{value}); } ``` 3. **获取属性值**: ```java public static Object getProperty(Object obj, String propertyName) { // 获取类 Class clazz = obj.getClass(); // 获取PropertyDescriptor PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName); // 获取读方法(get方法) Method getMethod = pd.getReadMethod(); // 获取属性值 return getMethod.invoke(obj, new Object[]{}); } ``` #### 总结 本文介绍了如何使用Java反射机制来创建get和set方法,并通过反射调用这些方法来访问对象的属性。这种方式虽然灵活,但在实际开发中应当谨慎使用,因为它可能会降低代码的性能和可维护性。了解反射机制的基本原理对于深入理解Java编程语言具有重要意义。


















import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.beans.PropertyDescriptor;
public class PropertyUtil {
public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName){
StringBuffer sb = new StringBuffer();//构建一个可变字符串用来构建方法名称
Method setMethod = null;
Method getMethod = null;
PropertyDescriptor pd =null;
try
{
Field f = clazz.getDeclaredField(propertyName);//根据字段名来获取字段
if(f==null){
//构建方法的后缀
String methodEnd = propertyName.substring(0,1).toLowerCase()+propertyName.substring(1);
sb.append("set"+methodEnd);//构建set方法
//构建set 方法
setMethod = clazz.getDeclaredMethod(sb.toString(),new Class[]{f.getType()} );
sb.delete(0, sb.length());//清空整个可变字符串
sb.append("get"+methodEnd);//构建get方法
//构建get 方法
getMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{});
//构建一个属性描述器 把对应属性 propertyName 的 get 和 set 方法保存到属性描述器中
pd = new PropertyDescriptor(propertyName,getMethod,setMethod);
}
}catch(Exception ex){

- 粉丝: 19
- 资源: 11
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- Python 实现Rosenblatt算法2
- 基于FPGA的智能输液监控系统:集成手动报警、液晶显示与精准输液管理
- VBA单元格多块区域分块控制.xlsm
- Access数据库基础与应用项目式教程(Access2019)项目数据库
- Java编程中常用类详解及其应用场景
- Cimatron四轴后处理GPP2:多系统兼容与定制化加工解决方案
- BMS电池管理系统中颖SH367309方案详解:硬件设计与嵌入式编程实战
- HTML+CSS+DIV网页设计与布局(第3版)(微课版)-PPT课件
- 西门子PLC S7-200 Smart伺服液压PID系统集成与优化方案
- HTML+CSS+DIV网页设计与布局(第3版)(微课版)-教案
- AIGC中文脚本语言-https://github.com/Magic-Abracadabra/AI-Chinese-Scripting-Language
- C#实现的MODBUS调试工具:涵盖主站从站调试,支持RTU/TCP/UDP协议
- MATLAB实现电力系统最优潮流计算中原对偶内点法的应用
- HTML+CSS+DIV网页设计与布局(第3版)(微课版)源代码及效果
- 机器学习中黏菌算法(SMA)优化SVM/SVR/LSSVM参数的技术详解与应用
- C语言实现山谷数判断算法解析与应用



- 1
- 2
- 3
- 4
前往页