深拷贝和浅拷贝
1. 有关clone()方法
来源:Object 通用方法中
public native int hashCode()
public boolean equals(Object obj)
protected native Object clone() throws CloneNotSupportedException
public String toString()
public final native Class<?> getClass()
protected void finalize() throws Throwable {}
public final native void notify()
public final native void notifyAll()
public final native void wait(long timeout) throws InterruptedException
public final void wait(long timeout, int nanos) throws InterruptedException
public final void wait() throws InterruptedException
由上图可知clone() 是 Object 的 protected 方法,它不是 public,一个类没有显式去重写 clone(),其它类就不能直接去调用该类实例的 clone() 方法。
注意:Cloneable 接口规定,如果一个类没有实现 Cloneable 接口又调用了 clone() 方法,就会抛出 CloneNotSupportedException。
2. 浅拷贝
拷贝对象和原始对象的引用类型引用同一个对象。
代码例子:
public class ShallowCloneExample implements Cloneable {
private int[] arr;
public ShallowCloneExample() {
arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
}
public void set(int index, int value) {
arr[index] = value;
}
public int get(int index) {
return arr[index];
}
@Override
protected ShallowCloneExample clone() throws CloneNotSupportedException {
return (ShallowCloneExample) super.clone(); //同一个引用
}
public static void main(String[] args) {
ShallowCloneExample e1 = new ShallowCloneExample();
ShallowCloneExample e2 = null;
try {
e2 = e1.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
e1.set(1, 333);
System.out.println(e2.get(1)); // 333
}
}
打断点查看数组引用是否一样:
set的时候:
get的时候:
所以可以知道公用同一个数组引用。
3. 深拷贝
拷贝对象和原始对象的引用类型引用不同对象。
代码实现:
public class DeepCloneExample implements Cloneable {
private int[] arr;
public DeepCloneExample() {
arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
}
public void set(int index, int value) {
arr[index] = value;
}
public int get(int index) {
return arr[index];
}
@Override
protected DeepCloneExample clone() throws CloneNotSupportedException {
DeepCloneExample result = (DeepCloneExample) super.clone();
result.arr = new int[arr.length]; //不同引用
for (int i = 0; i < arr.length; i++) {
result.arr[i] = arr[i];
}
return result;
}
public static void main(String[] args) {
DeepCloneExample e1 = new DeepCloneExample();
DeepCloneExample e2 = null;
try {
e2 = e1.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
e1.set(2, 333);
System.out.println(e2.get(2)); // 2
}
}
用同样的方法,查看可知两个对象使用不同的引用。