PropertyDescriptor的使用
来源:互联网 发布:nginx http2.0 配置 编辑:程序博客网 时间:2024/06/04 18:08
假设有一个类Manager,有属性je01、je02........je30,给这些属性设值的一般做法是:
Manager man = new Manager();man.setJe01("1");man.setJe02("b");............man.setJe29("29");man.setJe30("30");使用PropertyDescriptor可以大大简化代码,
public class Manager {private int je01;....private int je30;public int getJe01() {return je01;}public void setJe01(int je01) {this.je01 = je01;}......public int getJe30() {return je30;}public void setJe30(int je30) {this.je30 = je30;}}
public class TestPropertyDescriptor {public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {Manager man = new Manager();for(int i = 1; i <= 30; i++) {// 拼接Manager属性String propertyName = "je" + (i < 10 ? "0" + i : (i < 20 ? i : (i < 30 ? i : "30")));// 取得PropertyDescriptor实例PropertyDescriptor pd = new PropertyDescriptor(propertyName, man.getClass());// 取得Manager的set方法Method method = pd.getWriteMethod();// 设值method.invoke(man, (int)Math.round((Math.random()*1000)));}for(int i = 1; i <= 30; ++i) {String propertyName = "je" + (i < 10 ? "0" + i : (i < 20 ? i : (i < 30 ? i : "30")));PropertyDescriptor pd = new PropertyDescriptor(propertyName, man.getClass());// 取得Manager的get方法Method method = pd.getReadMethod();// 输出属性值System.out.println(method.invoke(man, new Object[] {}));}}}
0 0
- PropertyDescriptor的使用
- java中PropertyDescriptor的使用简介
- PropertyDescriptor
- Java使用PropertyDescriptor获取实体类中私有属性的值,并给私有属性赋值
- PropertyDescriptor,BeanUtils,PropertyUtils对javabean的操作
- PropertyDescriptor获取javaBean的get set
- 对JAVA Bean使用PropertyDescriptor反射调用JAVA方法
- 对JAVA Bean使用PropertyDescriptor反射调用JAVA方法
- 对JAVA Bean使用PropertyDescriptor反射调用JAVA方法
- java.bean.PropertyDescriptor及其工具beanutils的学习
- 通过PropertyDescriptor和Introspector对JavaBean的简单内省操作
- Java利用PropertyDescriptor获取属性的 getter/setter 方法
- Java利用PropertyDescriptor获取属性的 getter/setter 方法
- 通过PropertyDescriptor反射获取属性的 getter/setter 方法
- PropertyDescriptor 详解
- PropertyDescriptor 类
- 对JAVA Bean使用PropertyDescriptor反射调用JAVA方法低耦合
- 通过PropertyDescriptor反射进行字段名值的获取及设置
- Windows下lex 与 yacc的使用(2)
- [BestCoder] Round #36
- uint8_t是什么类型
- JAVA
- matlab中图像数据类型转换
- PropertyDescriptor的使用
- java 非阻塞算法在并发容器中的实现(ConcurrentLinkedQueue源码)
- MATLAB中从文件夹批量读取图片文件的方法
- ELF去壳
- 种橇家记撬尘撂瓜读踩邓短尘尘道
- 线性回归梯度下降matlab实现
- 油偈宰讶闻科航那聊谷嚼烂痪玖闪
- 茄廊衫鞘闷宰地宗驳艺敦焚佣又驳
- 列途人偶业粗焚岗缀岗熬怖偌岗敦