学习心得

来源:互联网 发布:python re group 编辑:程序博客网 时间:2024/06/09 22:01

一篇记录旁观导师写代码的心得。

代码具体是想要实现两个bean之间的合并,不是仅仅以一个bean复制到另一个bean当中。而是实现对当前bean字段判断满足为空条件则将引入bean的相应字段值赋给当前bean。

1.当前bean字段非空,则不做操作,进入下一个字段判断
2.当前bean字段为空,且引入bean字段不为空,做赋值操作。

考虑bean中带有list属性字段,需要递归判断list中的元素。

#代码具体方法实现块@SuppressWarnings({ "rawtypes", "unchecked" })    public void combine(InterBossSvcBean newBean) throws IllegalArgumentException, IllegalAccessException {        if(newBean == null)            return ;        Class thisCls = this.getClass();        Field[] lastfields = thisCls.getDeclaredFields();        for (Field f : lastfields) {            f.setAccessible(true);            Object v1 = f.get(this);            Object v2 = f.get(newBean);            if(v1 == null && v2 != null) {                f.set(this, v2);                 continue;            }            if(v2 == null)                continue;            System.out.println(f.getType());            if(f.getType().equals(int.class)) {                if(v1.equals(0) && !v2.equals(0)) {                    f.set(this, v2);                }            }            if(f.getType().equals(java.util.List.class)) {                List l1 = (List)v1;                List l2 = (List)v2;                Iterator i1 = l1.iterator();                Iterator i2 = l2.iterator();                while(true) {                    if(i1.hasNext() && i2.hasNext())                        ((InterBossSvcBean)i1.next()).combine((InterBossSvcBean)i2.next());                    else if(i1.hasNext())                        break;                    else if(i2.hasNext()) {                        l1.addAll(l2.subList(l2.indexOf(i2.next()), l2.size()));                        break;                    }                    else {                        break;                    }                }            }        }    }

查看源码一定是最快能够学习到如何调用已封装好的方法。
逻辑上分步骤考虑,代码上按步骤层层实现。
测试检查才可以按步骤分情形测试。

原创粉丝点击