这篇文章将为大家详细讲解有关利用Java怎么对对象进行操作,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
对象复制(反射法)
public static void copyProp(Object from, Object to, String... filterProp) { HashSet<String> filterSet = new HashSet<String>(Arrays.asList(filterProp)); Class<?> fromc = from.getClass(); Class<?> toc = to.getClass(); List<Field> to_fields = new ArrayList<Field>() ; 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怎么对对象进行操作-创新互联
标题URL:https://www.cdcxhl.com/article0/ccspio.html
成都网站建设公司_创新互联,为您提供微信小程序、品牌网站制作、域名注册、Google、软件开发、搜索引擎优化
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联