@x-power
2019-09-25T13:07:26.000000Z
字数 2791
阅读 706
常用函数
浅克隆: 被复制对象的所有基本变量都含有和原来变量完全相同的值, 而其他所有的引用对象任然指向原来的对象. 换言之, 浅克隆仅仅复制锁考虑的对象, 而不复制它所引用的对象. 克隆的深度仅仅到栈内存中.
深克隆: 和浅克隆不一样的点在于其重写了clone函数. 在克隆本身对象的时候, 也对其中的引用类型的属性进行克隆.
package reBegin;public class CloneDemo{public static void main(String[] args) throws CloneNotSupportedException {/* 浅克隆 */WeakTestClone testClone = new WeakTestClone(1,"阿狗", new GG(77));WeakTestClone cloneWeak = (WeakTestClone) testClone.clone();// 这样会将所有克隆体和被克隆体的引用改变, 因为getGg() 得到了栈内存地址, setAge()的时候改变了堆内存的地址.// 因为两个对象的引用属性是指向同一个堆内存地址的,所以两个对象的gg 属性就改变了.testClone.getGg().setAge(110);testClone.setName("JackMa");System.out.println(testClone);System.out.println(cloneWeak);/* 深克隆 */StrongTestClone strongTestClone = new StrongTestClone(1,"阿狗",new GG(77));StrongTestClone cloneStrong = (StrongTestClone) strongTestClone.clone();// StrongTestClone 重写了clone函数, 在其中将其 gg 属性也一并克隆.// 这个时候对象中的gg属性, 存储的堆内存地址就不一样了. 是两个完全不同的对象.strongTestClone.getGg().setAge(111);strongTestClone.setName("JackMa");System.out.println(strongTestClone);System.out.println(cloneStrong);}}/*** 浅克隆, 只会克隆 仅仅拷贝对象本身, 也就是拷贝的深度仅限于栈内存.*/class WeakTestClone implements Cloneable{private int age;private String name;private GG gg;WeakTestClone() {}WeakTestClone(int age, String name, GG gg) {this.age = age;this.name = name;this.gg = gg;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public GG getGg() {return gg;}public void setGg(GG gg) {this.gg = gg;}@Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();}@Overridepublic String toString() {return "testClone{" +"age=" + age +", name='" + name + '\'' +", gg=" + gg.getAge() +'}';}}class StrongTestClone implements Cloneable{private int age;private String name;private GG gg;public StrongTestClone() {}public StrongTestClone(int age, String name, GG gg) {this.age = age;this.name = name;this.gg = gg;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public GG getGg() {return gg;}public void setGg(GG gg) {this.gg = gg;}@Overridepublic String toString() {return "StrongTestClone{" +"age=" + age +", name='" + name + '\'' +", gg=" + gg.getAge() +'}';}@Overrideprotected Object clone() throws CloneNotSupportedException {StrongTestClone strongTestClone = (StrongTestClone) super.clone();strongTestClone.setGg((GG)gg.clone());return strongTestClone;}}class GG implements Cloneable{private int age;public GG() {}GG(int age) {this.age = age;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();}}
<!-- 结果 -->testClone{age=1, name='JackMa', gg=110}testClone{age=1, name='阿狗', gg=110}StrongTestClone{age=1, name='JackMa', gg=111}StrongTestClone{age=1, name='阿狗', gg=77}