通过反射执行get、set方法
生活随笔
收集整理的這篇文章主要介紹了
通过反射执行get、set方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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);// 執(zhí)行get方法返回值
Method setMethod = pd.getWriteMethod();//獲得set方法
setMethod?.invoke(sourceObj, newVal);
?
轉載于:https://www.cnblogs.com/BensonHe/p/4401752.html
總結
以上是生活随笔為你收集整理的通过反射执行get、set方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个十个字以内的个性签名。
- 下一篇: 21金维他价格是多少啊?