android根據屬性名獲取屬性值方法
阿新 • • 發佈:2019-02-08
private Object getFieldValueByName(String fieldName, Object o)
{
try
{
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getter = "get" + firstLetter + fieldName.substring(1);
Method method = o.getClass().getMethod(getter, new Class[] {});
Object value = method.invoke(o, new Object[] {});
return value;
} catch (Exception e)
{
System.out.println("屬性不存在");
return null;
}
}
{
try
{
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getter = "get" + firstLetter + fieldName.substring(1);
Method method = o.getClass().getMethod(getter, new Class[] {});
Object value = method.invoke(o, new Object[] {});
return value;
} catch (Exception e)
{
System.out.println("屬性不存在");
return null;
}
}