zoukankan      html  css  js  c++  java
  • 两个对象的属性赋值

    public class BeanCopyUtil {
        private static final Log log = LogFactory.getLog("FILE-ERROR");
        public static void copyField(Object targetObj, Object orginObj){
            if(targetObj == null || orginObj == null){
                log.error("BeanCopyUtil 属性拷贝的工具类出现null.[targetObj:"+targetObj+"]-[orginObj:"+orginObj+"]");
                return;
            }
            Class targetClass = targetObj.getClass();
    //        Field[] orgFields = orginObj.getClass().getFields();
    //        if(orgFields.length ==0){
            Field[] orgFields = orginObj.getClass().getDeclaredFields();
    //        }
            for (Field orgField : orgFields) {
                Field targetField;
                try {
                    targetField = targetClass.getDeclaredField(orgField.getName());
                } catch (Exception e) {
                    continue;
                }
                if (targetField != null) {
                    targetField.setAccessible(true);
                    orgField.setAccessible(true);
                    try {
                        targetField.set(targetObj, orgField.get(orginObj));
                    } catch (Exception e) {
                        continue;//当出现NoSuchFieldException异常时,直接下一次循环,这样就将origin中有的filed而target中没有的filed忽略
                    } finally {
                        targetField.setAccessible(false);
                        orgField.setAccessible(false);
                    }
                }
            }
        }
  • 相关阅读:
    5道趣味Python热身题【新手必学】
    操作系统特征
    二叉树的中序遍历
    英语一图画作文模板
    函数
    双阶乘与华里士公式
    因式分解
    【】连通图——详细解释
    【】this指针——c++中的特殊指针
    咱们程序员好用的云笔记
  • 原文地址:https://www.cnblogs.com/zhima-hu/p/7772390.html
Copyright © 2011-2022 走看看