java浅克隆与深克隆

浅克隆:

package testclone;

public class ShallowClone implements Cloneable {
	public String name;
	public String[] name2;

	public ShallowClone() {
		name2 = new String[2];
	}

	public Object clone() {
		ShallowClone o = null;
		try {
			o = (ShallowClone) super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return o;
	}
}


package testclone;

public class TestShallowClone {

	public void changeA(ShallowClone a) {
		a.name = "b";
	}

	public void changInt(int i) {
		i = i * 2 + 100;
	}

	public static void main(String[] args) {
		TestShallowClone test = new TestShallowClone();
		ShallowClone a = new ShallowClone();
		a.name = "a";
		System.out.println("before change : a.name=" + a.name);
		test.changeA(a);
		System.out.println("after  change : a.name=" + a.name);
		int i = 1;
		System.out.println("before change : i=" + i);
		test.changInt(i);
		System.out.println("after  change : i=" + i);

		ShallowClone a1 = new ShallowClone();
		ShallowClone a2 = (ShallowClone) a1.clone();
		a1.name2[0] = "a";
		a1.name2[1] = "1";
		a2.name2[0] = "b";
		a2.name2[1] = "1";
		System.out.println("a1.name2=" + a1.name2);
		System.out.println("a1.name2=" + a1.name2[0] + a1.name2[1]);
		System.out.println("a2.name2=" + a2.name2);
		System.out.println("a2.name2=" + a2.name2[0] + a2.name2[1]);
	}

}


输出:

before change : a.name=a
after  change : a.name=b
before change : i=1
after  change : i=1
a1.name2=[Ljava.lang.String;@61de33
a1.name2=b1
a2.name2=[Ljava.lang.String;@61de33
a2.name2=b1

深克隆:

package testclone;

public class DeepClone implements Cloneable {
	public String name;
	public String[] name2;

	public DeepClone() {
		name2 = new String[2];
	}

	public Object clone() {
		DeepClone deepClone = null;
		try {
			deepClone = (DeepClone) super.clone();
			if (name != null) {
				deepClone.name = new String(name);
			}
			if (name2 != null) {
				deepClone.name2 = new String[name2.length];
				for (int i = 0; i < name2.length; i++) {
					deepClone.name2[i] = this.name2[i];
				}
			}
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return deepClone;
	}
}

package testclone;

public class TestDeepClone {

	public void changeDeepClone(DeepClone a) {
		a.name = "b";
	}

	public void changInt(int i) {
		i = i * 2 + 100;
	}

	public static void main(String[] args) {
		TestDeepClone test = new TestDeepClone();
		DeepClone a = new DeepClone();
		a.name = "a";
		System.out.println("before change : a.name=" + a.name);
		test.changeDeepClone(a);
		System.out.println("after  change : a.name=" + a.name);
		int i = 1;
		System.out.println("before change : i=" + i);
		test.changInt(i);
		System.out.println("after  change : i=" + i);

		DeepClone a1 = new DeepClone();
		DeepClone a2 = (DeepClone) a1.clone();
		a1.name2[0] = "a";
		a1.name2[1] = "1";
		a2.name2[0] = "b";
		a2.name2[1] = "1";
		System.out.println("a1.name2=" + a1.name2);
		System.out.println("a1.name2=" + a1.name2[0] + a1.name2[1]);
		System.out.println("a2.name2=" + a2.name2);
		System.out.println("a2.name2=" + a2.name2[0] + a2.name2[1]);
	}

}

输出:

before change : a.name=a
after  change : a.name=b
before change : i=1
after  change : i=1
a1.name2=[Ljava.lang.String;@61de33
a1.name2=a1
a2.name2=[Ljava.lang.String;@14318bb
a2.name2=b1


你可能感兴趣的:(java浅克隆与深克隆)