下面让我们温习一下本章早些时候提出的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(散列表)举办深层复制,也必需采纳雷同的处理惩罚。
这个例子剩余的部门显示出克隆已实际举办——证据就是在克隆了工具今后,可以自由改变它,而本来谁人工具不受任何影响。
