Clone这个方法很鸡肋,原因是因为它是受保护的(protected),其二Clone这个方法是可选的,即有可能类没有提供这个方法。
clone没有调用构造函数,而且如果父类没有实现clone的话,子类永远也不可能正确实现clone.
总之,clone是个很头痛的函数,如果要正确实现,必须考虑到深考贝的情况,否则被克隆出来的对象就不是独立的,如,要写一个堆栈类的克隆函数,正确的实现方法是:
@Override public Stack clone() {
try {
Stack result = (Stack) super.clone();
result.elements = elements.clone();
return result;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
要注意对于final字段不能正常工作,clone结构与可变对象的final字段是不兼容的。
对于列散数组,仅仅递归调用的clone方法还不够,看书中正确的做法:
public class HashTable implements Cloneable {
private Entry[] buckets = ...;
private static class Entry {
final Object key;
Object value;
Entry next;
Entry(Object key, Object value, Entry next) {
this.key = key;
this.value = value;
this.next = next;
}
Entry deepCopy() {
return new Entry(key, value,
next == null ? null : next.deepCopy());
}
}
@Override public HashTable clone() {
try {
HashTable result = (HashTable) super.clone();
result.buckets = new Entry[buckets.length];
for (int i = 0; i < buckets.length; i++)
if (buckets[i] != null)
result.buckets[i] = buckets[i].deepCopy();
return result;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
... // Remainder omitted
}
简单的说,实现了cloneable的接口类必须要有一个public的clone。然后调用super.clone,再对本类中的字段进行深拷贝,并用新对象的引用代码原来对象的引用。
其实,能不用clone就最好不用,最推荐的是用拷贝构造函数实现对象的复制。