本帖最后由 刘春发 于 2012-6-14 22:26 编辑
public Object setProperty(Object obj,String propertyName,Object value) throws IntrospectionException,
IllegalArgumentException, IllegalAccessException, InvocationTargetException{
PropertyDescriptor pd = new PropertyDescriptor(propertyName,obj.getClass());
Method method = pd.getWriteMethod();
method.invoke(obj, value);
return obj;
}//这个是内省?
public Object setProperty2(Object obj,String propertyName,Object value) throws IllegalArgumentException,
IllegalAccessException, SecurityException, NoSuchFieldException {
Field field = obj.getClass().getDeclaredField(propertyName);
field.setAccessible(true);
field.set(obj, value);
return obj;
}//这个是反射?
还是有点糊涂,内省和反射的作用到底是什么?
|