国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java 反射通過get方法獲得屬性值

2019-11-06 06:53:50
字體:
供稿:網(wǎng)友
1、根據(jù)對象獲得所有字段的值public static void method(Object obj) {     try {          Class clazz = obj.getClass();          Field[] fields = obj.getClass().getDeclaredFields();//獲得屬性          for (Field field : fields) {               PRopertyDescriptor pd = new PropertyDescriptor(field.getName(),                         clazz);               Method getMethod = pd.getReadMethod();//獲得get方法               Object o = getMethod.invoke(obj);//執(zhí)行g(shù)et方法返回一個Object               System.out.println(o);          }     } catch (SecurityException e) {          e.printStackTrace();     } catch (IllegalArgumentException e) {          e.printStackTrace();     } catch (IntrospectionException e) {          e.printStackTrace();     } catch (IllegalaccessException e) {          e.printStackTrace();     } catch (InvocationTargetException e) {          e.printStackTrace();     }}2、通過對象和具體的字段名字獲得字段的值public static void method(Object obj, String filed) {     try {          Class clazz = obj.getClass();          PropertyDescriptor pd = new PropertyDescriptor(filed, clazz);          Method getMethod = pd.getReadMethod();//獲得get方法          if (pd != null) {               Object o = getMethod.invoke(obj);//執(zhí)行g(shù)et方法返回一個Object               System.out.println(o);          }     } catch (SecurityException e) {          e.printStackTrace();     } catch (IllegalArgumentException e) {          e.printStackTrace();     } catch (IntrospectionException e) {          e.printStackTrace();     } catch (IllegalAccessException e) {          e.printStackTrace();     } catch (InvocationTargetException e) {          e.printStackTrace();     }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 雷山县| 和静县| 岱山县| 宜城市| 奎屯市| 阳山县| 资溪县| 大兴区| 沭阳县| 曲水县| 长汀县| 靖宇县| 桐乡市| 临清市| 耒阳市| 沙坪坝区| 陆良县| 蓝山县| 闻喜县| 鄂伦春自治旗| 渝中区| 宁城县| 陆良县| 花莲市| 长乐市| 邻水| 镇安县| 文安县| 嘉祥县| 香格里拉县| 中山市| 方城县| 咸阳市| 彰化市| 定结县| 万年县| 田阳县| 应用必备| 德钦县| 嵩明县| 荣成市|