Java中clone的写法
Cloneable这个接口设计得十分奇葩,不符合正常人的使用习惯,然而用这个接口的人很多也很有必要,所以还是有必要了解一下这套扭曲的机制。以下内容来自于对Effective Java ed 2. item 11的整理。
Cloneable接口
我们的clone方法
需要实现Cloneable接口
-
将限制符改为public;
-
将它的返回类型设置成子类类型(可以这么做是因为java允许covariant return type);
-
接住CloneNotSupportedException并不再抛出(既然已经实现了Cloneable接口,就不会抛出这个异常,不然用户又要在那里try-catch半天)。
@Override public PhoneNumber clone() throws ... { try { return (PhoneNumber) super.clone(); } catch(CloneNotSupportedException e) { throw new AssertionError(); // Can't happen } }
只需要重写clone方法
-
限制符为protected;
-
不实现Cloneable;
-
抛出CloneNotSupportedException。
不同情景下的clone方法实现
案例一:Stack
public class Stack { private Object[] elements; private int size = 0; private static final int DEFAULT_INITIAL_CAPACITY = 16; public Stack() {...} public void push(Object e) {...} public Object pop() {...} private void ensureCapacity() {...} //omitted for simplicity }
@Override public Stack clone() { try { Stack result = (Stack) super.clone(); result.elements = elements.clone(); return result; } catch (CloneNotSupportedException e) { throw new AssertionError(); } }
两种方法的区别如下:(渣图……)
案例二:HashTable
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; } } ... // Remainder omitted }
如果我们照搬Stack的克隆方法,是否会有效呢?
@Override public HashTable clone() { try { HashTable result = (HashTable) super.clone(); result.buckets = buckets.clone(); return result; } catch (CloneNotSupportedException e) { throw new AssertionError(); } }
HashTable original = new HashTable(); original.put(x, y); HashTable cloned = original.clone();
original.remove(x); //cloned gets removed by one element too, but does not know of it!! if(cloned.size() > 0){ doSomething(); //Danger! It's actually empty!! }
如图:
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; // Recursively copy the linked list headed by this Entry 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 }
//Iteratively copy the linked list headed by this Entry Entry deepCopy() { Entry result = new Entry(key, value, next); for (Entry p = result; p.next != null; p = p.next) p.next = new Entry(p.next.key, p.next.value, p.next.next); return result; }
其他碎碎念
-
(非final类的)clone方法不应调用克隆后对象的nonfinal方法。若该类的子类重写了这个nonfinal方法,该方法有可能在子类创建完毕之前去调用它的一些方法/数据,可能会引起数据损坏。
-
如果类中有一个指向可变对象的final域,则以上的clone实现机制无法work,因为对象创建好以后无法再给final域assign一个值。
-
不可变类不应该支持clone,因为clone后的对象跟原对象没有区别。
-
其实一种比较好的方法是copy constructor或copy factory。它们没有Cloneable的那些奇葩性,不抛异常,而且可以搞定final域。
public Yum(Yum yum); //copy constructor public static Yum newInstance(Yum yum); //copy factory
一个更好的好处是,interface-based copy constructor或copy factory (称为conversion constructors / conversion factories)可以允许用户选择与原对象不同类的克隆对象。如HashSet s = ...; new TreeSet(s); //将HashSet转换成TreeSet