zoukankan      html  css  js  c++  java
  • 复制两个类的相同属性

    /**
    * 复制两个类的相同属性
    * 
    * @param source
    * @param dest
    * @throws Exception
    */
    public static void copySameProperty(Object source, Object dest) throws Exception {
    // 获取属性
    BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(), Object.class);
    PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();
    
    BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(), Object.class);
    PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();
    try {
    for (int i = 0; i < sourceProperty.length; i++) {
    
    for (int j = 0; j < destProperty.length; j++) {
    if (sourceProperty[i].getName().equals(destProperty[j].getName()) && sourceProperty[i].getPropertyType() == destProperty[j].getPropertyType()) {
    destProperty[j].getWriteMethod().invoke(dest, sourceProperty[i].getReadMethod().invoke(source));
    break;
    }
    }
    }
    } catch (Exception e) {
    throw new Exception("属性复制失败:" + e.getMessage() + destProperty + sourceProperty);
    }
    }
  • 相关阅读:
    PL/SQL编程急速上手
    MySQL编程
    T-SQL编程
    SQL入门,就这么简单
    前端工具配置(webpack 4、vue-cli 3)
    Vue-router
    Vue组件应用
    Vue.js应用基础
    Bootstrap应用核心
    一篇文章教会你jQuery应用
  • 原文地址:https://www.cnblogs.com/dzhou/p/11236237.html
Copyright © 2011-2022 走看看