从品牌网站建设到网络营销策划,从策略到执行的一站式服务
这篇文章将为大家详细讲解有关利用Java怎么对对象进行操作,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
对象复制(反射法)
public static void copyProp(Object from, Object to, String... filterProp) { HashSetfilterSet = 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(); } } }
成都网站建设公司地址:成都市青羊区太升南路288号锦天国际A座10层 建设咨询028-86922220
成都快上网科技有限公司-四川网站建设设计公司 | 蜀ICP备19037934号 Copyright 2020,ALL Rights Reserved cdkjz.cn | 成都网站建设 | © Copyright 2020版权所有.
专家团队为您提供成都网站建设,成都网站设计,成都品牌网站设计,成都营销型网站制作等服务,成都建网站就找快上网! | 成都网站建设哪家好? | 网站建设地图