下面讓我們復習一下本章早些時候提出的Vector例子。這一次Int2類是可以克隆的,所以能對Vector進行深層復制:
//: AddingClone.java
// You must go through a few gyrations to
// add cloning to your own class.
import java.util.*;
class Int2 implements Cloneable {
private int i;
public Int2(int ii) { i = ii; }
public void increment() { i++; }
public String toString() {
return Integer.toString(i);
}
public Object clone() {
Object o = null;
try {
o = super.clone();
} catch (CloneNotSupportedException e) {
System.out.println("Int2 can't clone");
}
return o;
}
}
// Once it's cloneable, inheritance
// doesn't remove cloneability:
class Int3 extends Int2 {
private int j; // Automatically duplicated
public Int3(int i) { super(i); }
}
public class AddingClone {
public static void main(String[] args) {
Int2 x = new Int2(10);
Int2 x2 = (Int2)x.clone();
x2.increment();
System.out.println(
"x = " + x + ", x2 = " + x2);
// Anything inherited is also cloneable:
Int3 x3 = new Int3(7);
x3 = (Int3)x3.clone();
Vector v = new Vector();
for(int i = 0; i < 10; i++ )
v.addElement(new Int2(i));
System.out.println("v: " + v);
Vector v2 = (Vector)v.clone();
// Now clone each element:
for(int i = 0; i < v.size(); i++)
v2.setElementAt(
((Int2)v2.elementAt(i)).clone(), i);
// Increment all v2's elements:
for(Enumeration e = v2.elements();
e.hasMoreElements(); )
((Int2)e.nextElement()).increment();
// See if it changed v's elements:
System.out.println("v: " + v);
System.out.println("v2: " + v2);
}
} ///:~
Int3自Int2繼承而來,並添加了一個新的基本類型成員int j。大家也許認為自己需要再次覆蓋clone(),以確保j得到復制,但實情並非如此。將Int2的clone()當作Int3的clone()調用時,它會調用Object.clone(),判斷出當前操作的是Int3,並復制Int3內的所有二進制位。只要沒有新增需要克隆的句柄,對Object.clone()的一個調用就能完成所有必要的復制——無論clone()是在層次結構多深的一級定義的。
至此,大家可以總結出對Vector進行深層復制的先決條件:在克隆了Vector後,必須在其中遍歷,並克隆由Vector指向的每個對象。為了對Hashtable(散列表)進行深層復制,也必須采取類似的處理。
這個例子剩余的部分顯示出克隆已實際進行——證據就是在克隆了對象以後,可以自由改變它,而原來那個對象不受任何影響。