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

首頁 > 編程 > Java > 正文

java 反射

2019-11-06 08:20:13
字體:
來源:轉載
供稿:網友
Method M= Class.getMethod("MN", class); Object obj = M.invoke(Object, parm);

class 返回類型 MN 方法名 M 方法對象 Object 方法MN 所在對象 parm MN 的參數

反射getMethod()調用類方法時,發生 NoSuchMethodException異常,后來上網發現getMethod()調用公共方法,不能反射調用私有方法,后來找到 getDeclaredField()能夠訪問本類中定義的所有方法。

1、根據對象獲得所有字段的值

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);//執行get方法返回一個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);//執行get方法返回一個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(); }}java中遍歷實體類屬性和類型,并賦值和獲取值import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Date;/** * 獲取實體類型的屬性名和類型 * * @author kou */public class GetModelNameAndType{ public static void testReflect(Object model) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException { // 獲取實體類的所有屬性,返回Field數組 Field[] field = model.getClass().getDeclaredFields(); // 獲取屬性的名字 String[] modelName = new String[field.length]; String[] modelType = new String[field.length]; for (int i = 0; i < field.length; i++) { // 獲取屬性的名字 String name = field[i].getName(); modelName[i] = name; // 獲取屬性類型 String type = field[i].getGenericType().toString(); modelType[i] = type; //關鍵。。。可訪問私有變量 field[i].setAccessible(true); //給屬性設置 field[i].set(model, field[i].getType().getConstructor(field[i].getType()).newInstance("kou")); // 將屬性的首字母大寫 name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1) .toUpperCase()); if (type.equals("class java.lang.String")) { // 如果type是類類型,則前面包含"class ",后面跟類名 Method m = model.getClass().getMethod("get" + name); // 調用getter方法獲取屬性值 String value = (String) m.invoke(model); if (value != null) { System.out.println("attribute value:" + value); } } if (type.equals("class java.lang.Integer")) { Method m = model.getClass().getMethod("get" + name); Integer value = (Integer) m.invoke(model); if (value != null) { System.out.println("attribute value:" + value); } } if (type.equals("class java.lang.Short")) { Method m = model.getClass().getMethod("get" + name); Short value = (Short) m.invoke(model); if (value != null) { System.out.println("attribute value:" + value); } } if (type.equals("class java.lang.Double")) { Method m = model.getClass().getMethod("get" + name); Double value = (Double) m.invoke(model); if (value != null) { System.out.println("attribute value:" + value); } } if (type.equals("class java.lang.Boolean")) { Method m = model.getClass().getMethod("get" + name); Boolean value = (Boolean) m.invoke(model); if (value != null) { System.out.println("attribute value:" + value); } } if (type.equals("class java.util.Date")) { Method m = model.getClass().getMethod("get" + name); Date value = (Date) m.invoke(model); if (value != null) { System.out.println("attribute value:" + value.toLocaleString()); } } } } public static void main(String[] args) { try { testReflect(new VO()); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江山市| 溧水县| 商水县| 宽甸| 克东县| 南江县| 阿克苏市| 礼泉县| 平昌县| 长海县| 咸宁市| 肃宁县| 肇庆市| 密云县| 桑植县| 秦皇岛市| 宁明县| 本溪| 锦州市| 浦城县| 玛沁县| 青田县| 宽城| 乐平市| 米易县| 白玉县| 和硕县| 抚州市| 灌云县| 浮梁县| 苗栗县| 金川县| 云和县| 永和县| 华蓥市| 邯郸市| 安远县| 河源市| 楚雄市| 青海省| 固阳县|