Field[] field = behavior.getClass().getDeclaredFields(); for (int i = 0; i < field.length; i++) { String name = field[i].getName(); name = name.substring(0, 1).toUpperCase() + name.substring(1);//找到首位改爲大寫形式 String type = field[i].getGenericType().toString();//字段類型 if (type.equals("class java.lang.String")) { Method m; String value; try { m = behavior.getClass().getMethod("get" + name); value = (String) m.invoke(behavior); if (value.equals("x")) {//value是獲取到的值 mapdata.put("behavior", name);//name是字段名 } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } }