淺談淺克隆(shallow clone)和 深克隆(deep clone)

區別就在因而否對對象中的引用變量所指向的對象進行拷貝。

1.淺克隆/淺複製/淺拷貝

  淺拷貝是指在拷貝對象時,對於基本數據類型的變量會從新複製一份,而對於引用類型的變量只是對引用進行拷貝,沒有對引用指向的對象進行拷貝。java

2.深克隆/深複製/深拷貝

  深拷貝是指在拷貝對象時,同時會對引用指向的對象進行拷貝。this

 

示例代碼:spa

  1 import java.util.Date;
  2 
  3 class Wife implements Cloneable {
  4     private String name;
  5     private Date birthday;
  6     
  7     public Wife(){
  8     }
  9     
 10     public Wife(String name, Date birthday) {
 11         this.name = name;
 12         this.birthday = birthday;
 13     }
 14 
 15     public Date getBirthday(){
 16         return birthday;
 17     }
 18     
 19     public String getName() {
 20         return name;
 21     }
 22     public void setName(String name) {
 23         this.name = name;
 24     }
 25 
 26     public Object clone() throws CloneNotSupportedException {
 27         return super.clone();
 28     }
 29 
 30     public String toString() {
 31         return "Wife [name=" + name + ", birthday=" + birthday + "]";
 32     }
 33     
 34 }
 35 
 36 class Husband implements Cloneable {
 37     private String name;
 38     private Wife wife;
 39     
 40     public Husband(){
 41     }
 42 
 43     public Husband(String name, Wife wife) {
 44         this.name = name;
 45         this.wife = wife;
 46     }
 47 
 48     public Wife getWife() {
 49         return wife;
 50     }
 51 
 52     public void setWife(Wife wife) {
 53         this.wife = wife;
 54     }
 55 
 56     public String getName() {
 57         return name;
 58     }
 59 
 60     public void setName(String name) {
 61         this.name = name;
 62     }
 63 
 64     /**
 65      * 淺克隆一個對象
 66      * @throws CloneNotSupportedException 
 67      */
 68     public Object shallowClone() throws CloneNotSupportedException {
 69         return super.clone();
 70     }
 71     /**
 72      * 利用串行化深克隆一個對象,把對象以及它的引用讀到流裏,在寫入其餘的對象
 73      * @return
 74      * @throws Exception
 75      */
 76     public Object deepClone() throws Exception {
 77         Husband husband = (Husband) super.clone();
 78         husband.wife = (Wife) this.wife.clone();
 79         return husband;
 80     }
 81 }
 82 
 83 public class Test {
 84     public static void main(String[] args) throws Exception{
 85         //淺克隆
 86         Wife w1 = new Wife("曹夫人", new Date());
 87         Husband h1 = new Husband("曹先生", w1);
 88         Husband h2 = (Husband) h1.shallowClone();
 89         
 90         System.out.println("原型對象:" + h1.getName() + "--" + h1.getWife());
 91         System.out.println("克隆對象:" + h2.getName() + "--" + h2.getWife());
 92         
 93         w1.setName("張夫人");
 94         
 95         System.out.println("\n--------------------------修改克隆對象的屬性值後---------------------------\n");
 96         
 97         System.out.println("原型對象:" + h1.getName() + "--" + h1.getWife());
 98         System.out.println("克隆對象:" + h2.getName() + "--" + h2.getWife());
 99         
100         System.out.println("\n---------------------------------------------------------------------\n");
101         
102         //深克隆
103         Wife w2 = new Wife("曹夫人", new Date());
104         Husband h3 = new Husband("曹先生", w2);
105         Husband h4 = (Husband) h3.deepClone();
106         
107         System.out.println("原型對象:" + h3.getName() + "--" + h3.getWife());
108         System.out.println("克隆對象:" + h4.getName() + "--" + h4.getWife());
109         
110         w2.setName("張夫人");
111         
112         System.out.println("\n--------------------------修改克隆對象的屬性值後---------------------------\n");
113         
114         System.out.println("原型對象:" + h3.getName() + "--" + h3.getWife());
115         System.out.println("克隆對象:" + h4.getName() + "--" + h4.getWife());
116         
117         
118     }
119 }

控制檯輸出:code

原型對象:曹先生--Wife [name=曹夫人, birthday=Mon Jan 15 18:00:35 CST 2018]
克隆對象:曹先生--Wife [name=曹夫人, birthday=Mon Jan 15 18:00:35 CST 2018]

--------------------------修改克隆對象的屬性值後---------------------------

原型對象:曹先生--Wife [name=張夫人, birthday=Mon Jan 15 18:00:35 CST 2018]
克隆對象:曹先生--Wife [name=張夫人, birthday=Mon Jan 15 18:00:35 CST 2018]

---------------------------------------------------------------------

原型對象:曹先生--Wife [name=曹夫人, birthday=Mon Jan 15 18:00:35 CST 2018]
克隆對象:曹先生--Wife [name=曹夫人, birthday=Mon Jan 15 18:00:35 CST 2018]

--------------------------修改克隆對象的屬性值後---------------------------

原型對象:曹先生--Wife [name=張夫人, birthday=Mon Jan 15 18:00:35 CST 2018]
克隆對象:曹先生--Wife [name=曹夫人, birthday=Mon Jan 15 18:00:35 CST 2018]
相關文章
相關標籤/搜索