Principles
functioning public clone method.
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object.
The general intent is that, for any object x, the expression
x.clone() != x will be true,
x.clone().getClass() == x.getClass() will be true,
but these are not absolute requirements. While it is typically the case that
x.clone().equals(x) will be true,
this is not an absolute requirement. Copying an object will typically entail creating a new instance of its class, but it may require copying of internal data structures as well. No constructors are called.
The return type of the method is subclass instead of the super class which means this is applied with covariant which is introduced from JRE 1.5. As the code shown below:
@Override public PhoneNumber clone() {
try {
return (PhoneNumber) super.clone();
} catch(CloneNotSupportedException e) {
throw new AssertionError(); // Can‘t happen
}
}
import java.util.Arrays;
import java.util.EmptyStackException;
public class Stack implements Cloneable {
private Object[] elements;
private int size = 0;
private static final int DEFAULT_INITIAL_CAPACITY = 16;
public Stack() {
this.elements = new Object[DEFAULT_INITIAL_CAPACITY];
}
public void push(Object e) {
ensureCapacity();
elements[size++] = e;
}
public Object pop() {
if (size == 0)
throw new EmptyStackException();
Object result = elements[--size];
elements[size] = null; // Eliminate obsolete reference
return result;
}
// Ensure space for at least one more element.
private void ensureCapacity() {
if (elements.length == size)
elements = Arrays.copyOf(elements, 2 * size + 1);
}
@Override
protected Object clone() throws CloneNotSupportedException {
try {
Stack result = (Stack) super.clone();
// Do recursive clone with a class.
result.elements = elements.clone();
return result;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}
Implement clone with recursive clone method.
public class Hashtable implements Cloneable {
private Entry[] buckets = new Entry[3];
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 String toString() {
return String.format("[key: %d, value: %s, next: %s]", key, value,
next);
}
}
public static void main(String[] args) {
Hashtable ht = new Hashtable();
Entry previous = null;
for (int i = 0; i < 3; i++) {
Entry e = new Entry(i, "v" + i, previous);
ht.buckets[i] = e;
}
Hashtable newHt = ht.clone();
newHt.buckets[newHt.buckets.length - 1] = new Entry(
newHt.buckets[newHt.buckets.length - 1].key,
newHt.buckets[newHt.buckets.length - 1].value
+ String.valueOf(1),
newHt.buckets[newHt.buckets.length - 1].next);
System.out.println("newHt");
for (int i = 0; i < newHt.buckets.length; i++) {
System.out.println(newHt.buckets[i]);
}
System.out.println("ht");
for (int i = 0; i < ht.buckets.length; i++) {
System.out.println(ht.buckets[i]);
}
}
@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();
}
}
}
To prevent a stack overflow caused by the lengthy stack frame for each element in the list from happening. We can implement the code like this below:
// 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;
}
Summary
Effective Java 11 Override clone judiciously,布布扣,bubuko.com
Effective Java 11 Override clone judiciously
原文:http://www.cnblogs.com/haokaibo/p/3585452.html