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]

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

要点

  • 我们可以使用赋值运算符复制对象,也可以使用 clone() 方法。

  • 赋值运算符有副作用,因为它只复制引用,底层对象保持不变。

  • 对于基本类型实例变量,clone() 方法没有副作用,因为在克隆过程中会创建一个新对象。

  • 如果 clone() 方法实现不当,对于对象作为实例变量的情况,也会有副作用,因为克隆对象拥有对引用的副本。这被称为浅拷贝。

  • 可以重写 clone() 方法以防止浅拷贝,可以分别创建实例变量对象并更新其属性。这被称为深拷贝。

更新于: 2020年6月18日

852 次浏览

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告