JAVA中的浅克隆和深克隆

作者: 冬天里的懒喵 | 来源:发表于2017-08-07 19:24 被阅读109次

    无论是在java面试过程中,还是在与各种老鸟交流的过程中,对java对象的深浅拷贝,都是一个绕不开的问题,这个问题看似很容易,却是大多数人用来区分小白的标准问题。现在对该问题进行说明。

    1.定义

    浅克隆(拷贝):复制一个对象的实例,但是这个对象中包含的其它的对象还是共用的。一般用super.clone()方法,clone的对象就是浅克隆。
    深克隆(拷贝):复制一个对象的实例,而且这个对象中包含的其它的对象也要复制一份。如果使用clone(),那么需要对clone方法进行重写,复制一个对象super.clone(),之后再一一对属性进行复制。这样显得非常冗余,幸好,在java中还可以通过流来实现。但是注意,对象需要实现Serializable接口。

    2.举例

    有如下类Husband, Husband又引用了Wife。

    类结构图

    Wife类

    package com.dhb.CloneTest;
    
    import lombok.Data;
    
    import java.io.Serializable;
    import java.util.Date;
    
    @Data
    public class Wife implements Serializable {
    
        private String name;
    
        private Date birthday;
    
        public Wife() {
            this.name = "思思";
            this.birthday = new Date();
        }
    
        public Wife(String name, Date birthday) {
            this.name = name;
            this.birthday = birthday;
        }
    }
    

    Husband类

    package com.dhb.CloneTest;
    
    import lombok.Data;
    
    import java.io.*;
    import java.util.Date;
    
    @Data
    public class Husband implements Cloneable,Serializable {
    
        private Wife wife;
    
        private Date birthday;
    
        public Husband() {
            this.wife = new Wife();
            this.birthday = new Date();
        }
    
        public Husband(Wife wife, Date birthday) {
            this.wife = wife;
            this.birthday = birthday;
        }
    
        public Object clone() {
            Husband husband = null;
            try {
                husband = (Husband) super.clone();
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }finally {
                return  husband;
            }
        }
    
        public Object  deepClone() throws IOException, ClassNotFoundException {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            return  ois.readObject();
        }
    
    }
    

    在Husband类中存在clone和deepClone方法。现在对这两个方法进行测试。

    package com.dhb.CloneTest;
    
    import java.io.IOException;
    
    public class CloneTest {
    
        public static void main(String[] args) {
    
            try {
                Husband husband = new Husband();
                System.out.println("husband birthday is :"+husband.getBirthday());
                System.out.println("wife birthday is :"+husband.getWife().getBirthday());
                System.out.println("***********************************************");
                Husband husband1 = (Husband) husband.clone();
                System.out.println("husband1 birthday is :"+husband1.getBirthday());
                System.out.println("wife birthday is :"+husband1.getWife().getBirthday());
                System.out.println("***********************************************");
                System.out.println("husband 是否相同:"+(husband == husband1));
                System.out.println("wife 是否相同:"+(husband.getWife() == husband1.getWife()));
    
                System.out.println("***********************************************");
                Husband husband2 = (Husband) husband.deepClone();
                System.out.println("husband2 birthday is :"+husband2.getBirthday());
                System.out.println("wife birthday is :"+husband2.getWife().getBirthday());
                System.out.println("***********************************************");
                System.out.println("husband 是否相同:"+(husband == husband2));
                System.out.println("wife 是否相同:"+(husband.getWife() == husband2.getWife()));
    
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
    
        }
    
    }
    

    运行结果:

    husband birthday is :Mon Aug 07 19:03:38 CST 2017
    wife birthday is :Mon Aug 07 19:03:38 CST 2017
    ***********************************************
    husband1 birthday is :Mon Aug 07 19:03:38 CST 2017
    wife birthday is :Mon Aug 07 19:03:38 CST 2017
    ***********************************************
    husband 是否相同:false
    wife 是否相同:true
    ***********************************************
    husband2 birthday is :Mon Aug 07 19:03:38 CST 2017
    wife birthday is :Mon Aug 07 19:03:38 CST 2017
    ***********************************************
    husband 是否相同:false
    wife 是否相同:false
    

    注:上述代码中的@Data 是lombak的一个注解。

    当然,实现深克隆的方法并不局限于流这一种办法,还可以通过json等其他办法实现。

    相关文章

      网友评论

        本文标题:JAVA中的浅克隆和深克隆

        本文链接:https://www.haomeiwen.com/subject/tlgalxtx.html