首页 > 其他 > 详细

通过反射执行get、set方法

时间:2015-04-08 12:25:16      阅读:409      评论:0      收藏:0      [点我收藏+]

Class clazz = sourceObj.getClass();

1、获取所有属性

BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();

2、获取指定属性
PropertyDescriptor pd = new PropertyDescriptor(fieldName, clazz);

Method getMethod = pd.getReadMethod();// 获得get方法
Object val = getMethod.invoke(sourceObj);// 执行get方法返回值

Method setMethod = pd.getWriteMethod();//获得set方法

setMethod .invoke(sourceObj, newVal);

 

通过反射执行get、set方法

原文:http://www.cnblogs.com/BensonHe/p/4401752.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!