新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

利用Java怎么对对象进行操作-创新互联

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

创新互联专注于企业网络营销推广、网站重做改版、丰都网站定制设计、自适应品牌网站建设、成都h5网站建设商城网站建设、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为丰都等各大城市提供网站开发制作服务。

对象复制(反射法)


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.wjwzjz.com/article/ccspio.html
在线咨询
服务热线
服务热线:028-86922220
TOP