• Java实现深拷贝和浅拷贝


    1.类实现Cloneable才可以进行对象拷贝

    2.Cloneable只实现浅拷贝,需要实现深拷贝的必须要重写clone()方法

    3.利用反序列化也可以实现深拷贝,但是反序列化耗时较长

    n.浅拷贝是指拷贝对象时只拷贝对象本身和其基本变量及引用变量,不拷贝对象中引用指向的对象,深拷贝反之

    4.可以浅拷贝的对象类

     1 package deepCopy;
     2 
     3 import java.io.Serializable;
     4 
     5 public class UserShallowClone implements Cloneable,Serializable{
     6     
     7     /**
     8      * 
     9      */
    10     private static final long serialVersionUID = 222;
    11     private int age;
    12     private String name;
    13     private House house;
    14     
    15     public UserShallowClone(int age, String name, House house) {
    16         super();
    17         this.age = age;
    18         this.name = name;
    19         this.house = house;
    20     }
    21     
    22     
    23     public int getAge() {
    24         return age;
    25     }
    26 
    27 
    28     public void setAge(int age) {
    29         this.age = age;
    30     }
    31 
    32 
    33     public String getName() {
    34         return name;
    35     }
    36 
    37 
    38     public void setName(String name) {
    39         this.name = name;
    40     }
    41 
    42 
    43     public House getHouse() {
    44         return house;
    45     }
    46 
    47 
    48     public void setHouse(House house) {
    49         this.house = house;
    50     }
    51 
    52 
    53     @Override
    54     protected Object clone() throws CloneNotSupportedException {
    55         // TODO Auto-generated method stub
    56         return super.clone();
    57     }
    58 
    59 }
    View Code

    5.可以深拷贝的对象类

     1 package deepCopy;
     2 
     3 public class UserDeepClone implements Cloneable{
     4 
     5     private int age;
     6     private String name;
     7     private House house;
     8     
     9     public UserDeepClone(int age, String name, House house) {
    10         super();
    11         this.age = age;
    12         this.name = name;
    13         this.house = house;
    14     }
    15     
    16     
    17     public int getAge() {
    18         return age;
    19     }
    20 
    21 
    22     public void setAge(int age) {
    23         this.age = age;
    24     }
    25 
    26 
    27     public String getName() {
    28         return name;
    29     }
    30 
    31 
    32     public void setName(String name) {
    33         this.name = name;
    34     }
    35 
    36 
    37     public House getHouse() {
    38         return house;
    39     }
    40 
    41 
    42     public void setHouse(House house) {
    43         this.house = house;
    44     }
    45 
    46 
    47     @Override
    48     protected Object clone() throws CloneNotSupportedException {
    49         UserDeepClone userNew = (UserDeepClone)super.clone();
    50         userNew.house = (House) house.clone();
    51         return userNew;
    52     }
    53     
    54 }
    View Code

    6.House类

     1 package deepCopy;
     2 
     3 import java.io.Serializable;
     4 
     5 public class House implements Cloneable,Serializable{
     6     
     7     /**
     8      * 
     9      */
    10     private static final long serialVersionUID = 33;
    11     private int zipCode;
    12 
    13     public House(int zipCode) {
    14         super();
    15         this.zipCode = zipCode;
    16     }
    17 
    18     public int getZipCode() {
    19         return zipCode;
    20     }
    21 
    22     public void setZipCode(int zipCode) {
    23         this.zipCode = zipCode;
    24     }
    25 
    26     @Override
    27     protected Object clone() throws CloneNotSupportedException {
    28         // TODO Auto-generated method stub
    29         return super.clone();
    30     }
    31     
    32     
    33 }
    View Code

    7.测试类

    package deepCopy;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
    public class Test {
        public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException
        {
            //deep copy
            long deepClone = System.currentTimeMillis();
            House house = new House(1010);
            UserDeepClone userDeep = new UserDeepClone(11, "CC", house);
            UserDeepClone userDeepCopy = (UserDeepClone) userDeep.clone();
            System.out.println("deep copy by cloneable speeds " +(System.currentTimeMillis()-deepClone) + "ms");
            System.out.println("Deep Copy----------");
            System.out.println("userDeep" + userDeep
                    + ",userSDeepCopy=" + userDeepCopy);
            System.out.println("userDeep house=" + userDeep.getHouse()
                    + ",userDeepCopy house=" + userDeepCopy.getHouse());
    
            System.out.println();
            System.out.println("Shallow Copy----------");
            
            //shallow copy
            long shallowCopy = System.currentTimeMillis();
            UserShallowClone userShallow = new UserShallowClone(12, "CC", house);
            UserShallowClone userShallowCopy = (UserShallowClone) userShallow.clone();
            System.out.println("shallowCopy spends " + (System.currentTimeMillis()-shallowCopy) + "ms");
            System.out.println("userShallow=" + userDeep
                    + ",userShallowCopy=" + userDeepCopy);
            System.out.println("userShallow house=" + userShallow.getHouse()
                    + ",userShallowCopy house=" + userShallowCopy.getHouse());
            //反序列化深拷贝
            System.out.println();
            long deepSer = System.currentTimeMillis();
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(userShallow);
            
            ObjectInputStream oi = new ObjectInputStream(new ByteArrayInputStream(bo.toByteArray()));
            UserShallowClone  userDeepSer = (UserShallowClone) oi.readObject();
            System.out.println("deep copy by ser spends " + (System.currentTimeMillis()-deepSer) + "ms");
            System.out.println("deep copy by ser--------------------");
            System.out.println("userShallow=" + userShallow
                    +",userDeepSer=" + userDeepSer);
            System.out.println("userShallow house=" + userShallow.getHouse()
                    +",userDeepSer house=" + userDeepSer.getHouse());
        }
    }
    View Code

    8.测试结果

  • 相关阅读:
    子序列自动机
    poj 暴力水题
    小白贪心题
    组合数+费马大/小定理
    随机数法
    vector的二维用法+前缀和
    巨思维题
    思维水题
    Codeforces Round #323 (Div. 2) D.Once Again... (nlogn LIS)
    Codeforces Round #325 (Div. 2) D. Phillip and Trains (BFS)
  • 原文地址:https://www.cnblogs.com/cici20166/p/6528482.html
Copyright © 2020-2023  润新知