/ **
* Gets an object property that returns an array of strings
*
* @param objects O
* @return String [] array of strings
* /
Private static String [] getFiledName (Object O)
{
the try
{
Field, [] = O Fields. getClass () getDeclaredFields ();.
String [] = the fieldNames new new String [fields.length];
for (int I = 0; I <fields.length; I ++)
{
the fieldNames [I] = Fields [I] .getName () ;
}
return the fieldNames;
} the catch (a SecurityException E)
{
e.printStackTrace ();
System.out.println (e.toString ());
}
return null;
}
/ **
* The use of getting a property value reflecting property name
*
* @param fieldName 属性名称
* @param o 操作对象
* @return Object 属性值
*/
private static 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;
}
}
java get object property values and property name
Guess you like
Origin www.cnblogs.com/Mr-xy/p/12349740.html
Recommended
Ranking