当前位置: 移动技术网 > IT编程>开发语言>Java > JAVA 深层拷贝 DeepCopy的使用详解

JAVA 深层拷贝 DeepCopy的使用详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

仪表设备,遗失过往,爱尔兰不许离婚

方法实现很简单,提供两种方式:
一种是序列化成数据流,前提是所有对象(对象中包含的对象...)都需要继承serializable接口,如果都继承了那很容易,如果没有继承,而且也不打算修改所有类,可以用第二种方式。

第二种是将对象序列化为json,通过json来实现拷贝,这种方式需要用到net.sf.json.jsonobject。
具体代码如下:

复制代码 代码如下:

    public class deepcopy { 
        /**
         * 深层拷贝
         * 
         * @param <t>
         * @param obj
         * @return
         * @throws exception
         */ 
        public static <t> t copy(t obj) throws exception { 
            //是否实现了序列化接口,即使该类实现了,他拥有的对象未必也有... 
            if(serializable.class.isassignablefrom(obj.getclass())){ 
                //如果子类没有继承该接口,这一步会报错 
                try { 
                    return copyimplserializable(obj); 
                } catch (exception e) { 
                    //这里不处理,会运行到下面的尝试json 
                } 
            } 
            //如果序列化失败,尝试json序列化方式 
            if(hasjson()){ 
                try { 
                    return copybyjson(obj); 
                } catch (exception e) { 
                    //这里不处理,下面返回null 
                } 
            } 
            return null; 
        } 

        /**
         * 深层拷贝 - 需要类继承序列化接口
         * @param <t>
         * @param obj
         * @return
         * @throws exception
         */ 
        @suppresswarnings("unchecked") 
        public static <t> t copyimplserializable(t obj) throws exception { 
            bytearrayoutputstream baos = null; 
            objectoutputstream oos = null; 

            bytearrayinputstream bais = null; 
            objectinputstream ois = null; 

            object o = null; 
            //如果子类没有继承该接口,这一步会报错 
            try { 
                baos = new bytearrayoutputstream(); 
                oos = new objectoutputstream(baos); 
                oos.writeobject(obj); 
                bais = new bytearrayinputstream(baos.tobytearray()); 
                ois = new objectinputstream(bais); 

                o = ois.readobject(); 
                return (t) o; 
            } catch (exception e) { 
                throw new exception("对象中包含没有继承序列化的对象"); 
            } finally{ 
                try { 
                    baos.close(); 
                    oos.close(); 
                    bais.close(); 
                    ois.close(); 
                } catch (exception e2) { 
                    //这里报错不需要处理 
                } 
            } 
        } 

        /**
         * 是否可以使用json
         * @return
         */ 
        private static boolean hasjson(){ 
            try { 
                class.forname("net.sf.json.jsonobject"); 
                return true; 
            } catch (exception e) { 
                return false; 
            } 
        } 

        /**
         * 深层拷贝 - 需要net.sf.json.jsonobject
         * @param <t>
         * @param obj
         * @return
         * @throws exception
         */ 
        @suppresswarnings("unchecked") 
        public static <t> t copybyjson(t obj) throws exception { 
            return (t)jsonobject.tobean(jsonobject.fromobject(obj),obj.getclass()); 
        } 
    } 

只需要调用copy方法就行。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网