/**
* 将所有属性值设置为 null
* @throws SecurityException
* @throws NoSuchMethodException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
@SuppressWarnings("unchecked")
public void clean() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Class thisClass = this.getClass();
Field[] fields = thisClass.getFields();
for (Field field : fields) {
String fieldName = field.getName();
String stringLetter = fieldName.substring(0, 1).toUpperCase();
Method setMethod = thisClass.getMethod(
"set" + stringLetter.substring(1) + fieldName, new Class[] { field.getType() });
setMethod.invoke(thisClass, new Object[]{null});
}
}
反射小代码
猜你喜欢
转载自wanglei2304202013.iteye.com/blog/1481817
今日推荐
周排行