利用Java怎么对对象进行操作-创新互联
这篇文章将为大家详细讲解有关利用Java怎么对对象进行操作,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

对象复制(反射法)
public static void copyProp(Object from, Object to, String... filterProp) {
HashSet filterSet = new HashSet(Arrays.asList(filterProp));
Class<?> fromc = from.getClass();
Class<?> toc = to.getClass();
List to_fields = new ArrayList() ;
while (toc != null) {
to_fields.addAll(Arrays.asList(toc.getDeclaredFields()));
toc = toc.getSuperclass();
}
for (Field to_field : to_fields) {
try{
if (filterSet.contains(to_field.getName())||"serialVersionUID".equals(to_field.getName())) {
continue;
}
Field from_field = null;
try{
from_field = fromc.getDeclaredField(to_field.getName());
}catch (Exception e){
continue;
}
from_field.setAccessible(true);
Object value = from_field.get(from);
if(value==null){
continue;
}
to_field.setAccessible(true);
to_field.set(to, value);
}catch (Exception e){
e.printStackTrace();
}
}
} 分享题目:利用Java怎么对对象进行操作-创新互联
转载来于:http://www.jxjierui.cn/article/ccspio.html


咨询
建站咨询
