更多 Java 高级知识方面的文章,请参见文集《Java 高级知识》
首先看下面这一段代码:
Student s = new Student();
Student s1= s;
- 如果是在 C++ 中,会调用拷贝构造函数,复制一份新的对象,由
s1
指向该新的对象 - 如果是在 Java 中,
s1
为引用,仍然指向s
的内存区域,即同一份对象
在下面的代码中:
c1
和 c2
刚开始分别指向两个不同的对象,c2 = c1;
使得 c2
指向 c1
的对象,因此 c2
原本指向的对象 B 失去引用,会被 GC 回收。
public class Clone_Test {
public static void main(String[] args) {
MyClone c1 = new MyClone("A");
MyClone c2 = new MyClone("B");
// c2 指向 c1 的对象
// c2 原本指向的对象 B 失去引用,会被 GC 回收
c2 = c1;
c2.name = "C";
System.out.println(c1.name); // 输出 C
System.out.println(c2.name); // 输出 C
}
}
class MyClone {
public String name;
public MyClone(String name) {
this.name = name;
}
}
Clone 的实现
实现 Cloneable
接口,并重写 clone()
方法。
关于 Cloneable 接口
-
Cloneable
是一个空接口。并不包含任何方法,更像是一个标记。
public interface Cloneable {
}
-
implements Cloneable
指示调用clone()
方法时可以合法地对该对象进行按字段的复制
A class implements the <code>Cloneable</code> interface to indicate to the clone() method that it is legal for that method to make a field-for-field copy of instances of that class.
- 如果不实现
Cloneable
接口,此时在调用clone()
方法时,会抛出异常CloneNotSupportedException
关于 Clone 方法
-
clone()
方法是Object
类中自带的 native 方法,参见 Java Object 类中有哪些方法 -
clone()
方法默认是浅复制- 对基础类型有用
- 对引用类型只是复制引用,不是真正地复制对象
-
clone()
方法不会调用构造方法 - 几个基本的比较:
x.clone() == x // false,因为创建出了一个新对象
x.clone().getClass() == x.getClass() // true
x.clone().equals(x) // 一般为false,取决于 clone() 方法的具体实现
例如将上述的代码重构为:
public class Clone_Test {
public static void main(String[] args) {
MyClone c1 = new MyClone("A");
MyClone c2 = new MyClone("B");
// c2 指向一个新对象
// c2 原本指向的对象 B 失去引用,会被 GC 回收
c2 = (MyClone) c1.clone();
c2.name = "C";
System.out.println(c1.name); // 输出 A
System.out.println(c2.name); // 输出 C
}
}
class MyClone implements Cloneable {
public String name;
public MyClone(String name) {
this.name = name;
}
public Object clone() {
MyClone c = null;
try {
c = (MyClone) super.clone();
} catch (Exception e) {
}
return c;
}
}
浅拷贝 VS 深拷贝
Object
类中自带的clone()
方法是浅拷贝,即 field-for-field copy。
如果某个对象中包含一些其他对象,例如 Stream
,浅拷贝只会复制该 Stream
对象的引用,该 Stream
对象会被两个对象 c1
和 c2
共享,可能会导致问题。
要想实现深拷贝,即对象中包含的其他对象也会复制出一份新的对象,可以通过如下方式:
- 需要重写
clone()
方法,实现深拷贝 - 通过序列化方式
ObjectOutputStream ObjectInputStream
- 通过第三方库