在做对象克隆,尤其是深度克隆时,需要注意不仅仅对象的引用需要特殊处理,数组也一样
public class ArrayCloneTest implements Cloneable{
public byte[] source = new byte[] {(byte)127,(byte)1};
@Override
public ArrayCloneTest clone() throws CloneNotSupportedException {
ArrayCloneTest gen = (ArrayCloneTest)super.clone();
return gen;
}
public static void main(String[] args) {
ArrayCloneTest test = new ArrayCloneTest();
System.out.println(Arrays.toString(test.source));
try {
ArrayCloneTest clone = test.clone();
clone.source[1] =(byte)2;
System.out.println(Arrays.toString(clone.source));
System.out.println(Arrays.toString(test.source));
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}