Java 中的 clone() 方法


Java 提供了賦值運算子來複制值,但沒有運算子來複制物件。Object 類有一個 clone 方法,可用於複製物件的值,而不會產生任何副作用。賦值運算子有一個副作用,即當一個引用被賦值給另一個引用時,不會建立新的物件,並且這兩個引用都指向同一個物件。這意味著如果我們在一個物件中更改值,則另一個物件中也會反映相同的更改。clone() 方法解決了這個問題。請參見下面的示例。

示例

線上演示

public class Tester {
   public static void main(String[] args) throws CloneNotSupportedException {
      //Scenario 1: Using assignment operator to copy objects
      A a1 = new A();
      a1.a = 1;
      a1.b = 2;
      //Print a1 object
      System.out.println("a1: [" + a1.a + ", " + a1.b + "]");

      //assignment operator copies the reference
      A a2 = a1;
      //a2 and a1 are now pointing to same object
      //modify a2 and changes will reflect in a1
      a2.a = 3;
      System.out.println("a1: [" + a1.a + ", " + a1.b + "]");
      System.out.println("a2: [" + a2.a + ", " + a2.b + "]");

      //Scenario 2: Using cloning, we can prevent the above problem
      B b1 = new B();
      b1.a = 1;
      b1.b = 2;

      //Print b1 object
      System.out.println("b1: [" + b1.a + ", " + b1.b + "]");

      //cloning method copies the object
      B b2 = b1.clone();

      //b2 and b1 are now pointing to different object
      //modify b2 and changes will not reflect in b1
      b2.a = 3;
      System.out.println("b1: [" + b1.a + ", " + b1.b + "]");
      System.out.println("b2: [" + b2.a + ", " + b2.b + "]");
   }
}

class A {
   public int a;
   public int b;
}

class B implements Cloneable {    
   public int a;
   public int b;

   public B clone() throws CloneNotSupportedException {
      B b = (B)super.clone();
      return b;
   }
}

輸出

a1: [1, 2]
a1: [3, 2]
a2: [3, 2]
b1: [1, 2]
b1: [1, 2]
b2: [3, 2]

要點

  • 我們可以使用賦值運算子複製物件,也可以使用 clone() 方法。

  • 賦值運算子有副作用,因為它只複製引用,底層物件保持不變。

  • 對於基本型別例項變數,clone() 方法沒有副作用,因為在克隆過程中會建立一個新物件。

  • 如果 clone() 方法實現不當,對於物件作為例項變數的情況,也會有副作用,因為克隆物件擁有對引用的副本。這被稱為淺複製。

  • 可以重寫 clone() 方法以防止淺複製,可以分別建立例項變數物件並更新其屬性。這被稱為深複製。

更新於: 2020年6月18日

852 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.