java对象深浅拷贝之实现探讨

Java对象深浅拷贝之实现探讨

1.什么是对象的深浅拷贝

       实际项目的开发中,经常会遇到这种情况,在不影响原有对象极其内容的前提下,去产生一个该对象的副本并继续后续的逻辑。此时,就出现对象拷贝的概念。对象拷贝(Object Copy)分为浅拷贝(Shallow Copy))和深拷贝(Deep Copy)。所谓浅拷贝,是指拷贝对象只复制了原对象的引用,,而非其值,换言之,拷贝对象和原对象仍然指向同一个地址和同一个实例。下图可以演示浅拷贝的模型。
java对象深浅拷贝之实现探讨_第1张图片
       我们不难看出,如果使用对象浅拷贝后,我们对拷贝对象进行属性的修改,势必会影响原来的对象。由于浅拷贝存在这样的问题,就产生深拷贝的概念。深拷贝是复制了原对象的值,所以拷贝对象与原对象完全独立,互不影响。深拷贝对象属性的修改不会影响原对象的属性内容。深拷贝的模型可以用下图演示。
java对象深浅拷贝之实现探讨_第2张图片

2.浅拷贝实现

实现方式1:复制构造方法

package com.wzy.test;

/**
 * @ClassName: ShallowCopyDemo
 * @Description:复制构造函数实现对象的浅拷贝
 * @author: wangzy
 * @date: 2019年5月11日 下午12:31:12
 */
public class ShallowCopyDemo {

	public static void main(String[] args) {
		GearBox gb = new GearBox("8AT", "爱信"); 
		SaloonCar sc1 = new SaloonCar("白色",3,gb);
		SaloonCar sc2 = new SaloonCar(sc1);
		System.out.println("sc1:"+sc1);
		System.out.println("sc2:"+sc2);
		System.out.println("----------------------------------------------------------");
		sc2.setAge(6);
		gb.setType("6AT");
		System.out.println("sc1:"+sc1);
		System.out.println("sc2:"+sc2);
		
	}

	/**
	 * 豪华轿车类
	 */
	static class SaloonCar {
		// 属性
		private String color; // 颜色
		private int age; // 车龄
		private GearBox gearBox; // 变速箱
		// 复制构造方法

		public SaloonCar(SaloonCar sc) {
			this.color = sc.getColor();
			this.age = sc.getAge();
			this.gearBox = sc.getGearBox();
		}

		public SaloonCar(String color, int age, GearBox gearBox) {
			this.color = color;
			this.age = age;
			this.gearBox = gearBox;
		}

		public String getColor() {
			return color;
		}

		public void setColor(String color) {
			this.color = color;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

		public GearBox getGearBox() {
			return gearBox;
		}

		public void setGearBox(GearBox gearBox) {
			this.gearBox = gearBox;
		}

		@Override
		public String toString() {
			return "SaloonCar [color=" + color + ", age=" + age + ", gearBox=" + gearBox + "]";
		}

	}

	/**
	 * 变速箱类
	 */
	static class GearBox {
		// 属性
		private String type;// 变速箱类型
		private String manufacturer; // 生产厂商

		public GearBox(String type, String manufacturer) {
			this.type = type;
			this.manufacturer = manufacturer;
		}

		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getManufacturer() {
			return manufacturer;
		}

		public void setManufacturer(String manufacturer) {
			this.manufacturer = manufacturer;
		}

		@Override
		public String toString() {
			return "GearBox [type=" + type + ", manufacturer=" + manufacturer + "]";
		}

	}
}

运行结果:

sc1:SaloonCar [color=白色, age=3, gearBox=GearBox [type=8AT, manufacturer=爱信]]
sc2:SaloonCar [color=白色, age=3, gearBox=GearBox [type=8AT, manufacturer=爱信]]
----------------------------------------------------------
sc1:SaloonCar [color=白色, age=3, gearBox=GearBox [type=6AT, manufacturer=爱信]]
sc2:SaloonCar [color=白色, age=6, gearBox=GearBox [type=6AT, manufacturer=爱信]]

实现方式2:重写clone方法

package com.wzy.test;

import javax.sound.sampled.SourceDataLine;

/**   
 * @ClassName:  SCDemo   
 * @Description:重写clone方法实现浅拷贝  
 * @author: wangzy
 * @date:   2019年5月11日 下午10:45:05   
 */
public class SCDemo {
	//测试
	public static void main(String[] args) {
		GearBox gb = new GearBox("DCT", "采埃孚");
		SaloonCar sc1 = new SaloonCar("雪光白", 1, gb);
		SaloonCar sc2 = (SaloonCar)sc1.clone();
		System.out.println("sc1:"+sc1);
		System.out.println("sc2:"+sc2);
		System.out.println("----------------------------------------------------------");
		sc2.setColor("红色");
		gb.setType("CVT");
		System.out.println("sc1:"+sc1);
		System.out.println("sc2:"+sc2);
	}
	
	
	/**
	 * 豪华轿车类
	 */
	static class SaloonCar implements Cloneable{
		// 属性
		private String color; // 颜色
		private int age; // 车龄
		private GearBox gearBox; // 变速箱

		public SaloonCar(String color, int age, GearBox gearBox) {
			this.color = color;
			this.age = age;
			this.gearBox = gearBox;
		}

		public String getColor() {
			return color;
		}

		public void setColor(String color) {
			this.color = color;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

		public GearBox getGearBox() {
			return gearBox;
		}

		public void setGearBox(GearBox gearBox) {
			this.gearBox = gearBox;
		}

		@Override
		public String toString() {
			return "SaloonCar [color=" + color + ", age=" + age + ", gearBox=" + gearBox + "]";
		}
		@Override
		public Object clone() {
			Object obj = null;
			try {
				obj = super.clone();
			} catch (CloneNotSupportedException e) {
				e.printStackTrace();
			}
			return obj;
		}

	}

	/**
	 * 变速箱类
	 */
	static class GearBox {
		// 属性
		private String type;// 变速箱类型
		private String manufacturer; // 生产厂商

		public GearBox(String type, String manufacturer) {
			this.type = type;
			this.manufacturer = manufacturer;
		}

		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getManufacturer() {
			return manufacturer;
		}

		public void setManufacturer(String manufacturer) {
			this.manufacturer = manufacturer;
		}

		@Override
		public String toString() {
			return "GearBox [type=" + type + ", manufacturer=" + manufacturer + "]";
		}

	}
}

运行结果

sc1:SaloonCar [color=雪光白, age=1, gearBox=GearBox [type=DCT, manufacturer=采埃孚]]
sc2:SaloonCar [color=雪光白, age=1, gearBox=GearBox [type=DCT, manufacturer=采埃孚]]
----------------------------------------------------------
sc1:SaloonCar [color=雪光白, age=1, gearBox=GearBox [type=CVT, manufacturer=采埃孚]]
sc2:SaloonCar [color=红色, age=1, gearBox=GearBox [type=CVT, manufacturer=采埃孚]]

       通过上述两种方式可以看出,对象浅拷贝的过程中,对于基本类型的属性是通过值传递的,对于引用数据类型,是通过地址传递的。但是针对String这种引用类型的对象是存放在内存常量池的,其值是无法修改的,换言之,对于String类型的属性set值并不是进行了值传递,而是重新创建了新的对象。

3.深拷贝的实现

方式一:重写clone方法

package com.wzy.test;

/**   
 * @ClassName:  DeepCopyDemo   
 * @Description:重写clone方法实现对象深拷贝   
 * @author: wangzy
 * @date:   2019年5月11日 下午11:35:40   
 */
public class DeepCopyDemo {
	
	public static void main(String[] args) {
		GearBox gb = new GearBox("DCT", "采埃孚");
		SaloonCar sc1 = new SaloonCar("雪光白", 1, gb);
		SaloonCar sc2 = (SaloonCar)sc1.clone();
		System.out.println("sc1:"+sc1);
		System.out.println("sc2:"+sc2);
		System.out.println("----------------------------------------------------------");
		sc2.setAge(3);
		sc2.setColor("黑色");
		gb.setType("CVT");
		System.out.println("sc1:"+sc1);
		System.out.println("sc2:"+sc2);
	}
	
	/**
	 * 豪华轿车类
	 */
	static class SaloonCar implements Cloneable{
		// 属性
		private String color; // 颜色
		private int age; // 车龄
		private GearBox gearBox; // 变速箱

		public SaloonCar(String color, int age, GearBox gearBox) {
			this.color = color;
			this.age = age;
			this.gearBox = gearBox;
		}

		public String getColor() {
			return color;
		}

		public void setColor(String color) {
			this.color = color;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

		public GearBox getGearBox() {
			return gearBox;
		}

		public void setGearBox(GearBox gearBox) {
			this.gearBox = gearBox;
		}

		@Override
		public String toString() {
			return "SaloonCar [color=" + color + ", age=" + age + ", gearBox=" + gearBox + "]";
		}
		@Override
		public Object clone() {
			Object obj = null;
			try {
				obj = super.clone();
			} catch (CloneNotSupportedException e) {
				e.printStackTrace();
			}
			
			SaloonCar sc = (SaloonCar)obj;
			sc.gearBox = (GearBox)sc.getGearBox().clone(); //调用内层clone方法
			return obj;
		}

	}

	/**
	 * 变速箱类
	 */
	static class GearBox implements Cloneable{
		// 属性
		private String type;// 变速箱类型
		private String manufacturer; // 生产厂商

		public GearBox(String type, String manufacturer) {
			this.type = type;
			this.manufacturer = manufacturer;
		}

		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getManufacturer() {
			return manufacturer;
		}

		public void setManufacturer(String manufacturer) {
			this.manufacturer = manufacturer;
		}

		@Override
		public String toString() {
			return "GearBox [type=" + type + ", manufacturer=" + manufacturer + "]";
		}
		@Override
		public Object clone() {
			Object obj = null;
			try {
				obj = super.clone();
			} catch (CloneNotSupportedException e) {
				e.printStackTrace();
			}
			return obj;
		}

	}
}

运行结果

sc1:SaloonCar [color=雪光白, age=1, gearBox=GearBox [type=DCT, manufacturer=采埃孚]]
sc2:SaloonCar [color=雪光白, age=1, gearBox=GearBox [type=DCT, manufacturer=采埃孚]]
----------------------------------------------------------
sc1:SaloonCar [color=雪光白, age=1, gearBox=GearBox [type=CVT, manufacturer=采埃孚]]
sc2:SaloonCar [color=黑色, age=3, gearBox=GearBox [type=DCT, manufacturer=采埃孚]]

方式二:通过对象序列化和反序列化实现

package com.wzy.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**   
 * @ClassName:  DCDemo   
 * @Description:对象序列化与反序列化实现对象深拷贝  
 * @author: wangzy
 * @date:   2019年5月11日 下午11:55:49   
 */
public class DCDemo {
	
	public static void main(String[] args) {
		
		try {
			GearBox gb = new GearBox("6AT", "爱信");
			SaloonCar sc1 = new SaloonCar("白色",3,gb);
			ByteArrayOutputStream bis = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(bis);
			oos.writeObject(sc1);
			oos.flush();
			ObjectInputStream input = new ObjectInputStream(new ByteArrayInputStream(bis.toByteArray()));
			SaloonCar sc2;
			sc2 = (SaloonCar)input.readObject();
			System.out.println("sc1:"+sc1);
			System.out.println("sc2:"+sc2);
			System.out.println("----------------------------------------------------------");
			sc2.setAge(5);
			sc2.setColor("黑色");
			gb.setType("CVT");
			System.out.println("sc1:"+sc1);
			System.out.println("sc2:"+sc2);
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 豪华轿车类
	 */
	static class SaloonCar implements Serializable{
		private static final long serialVersionUID = -3415928890915889093L;
		// 属性
		private String color; // 颜色
		private int age; // 车龄
		private GearBox gearBox; // 变速箱
		// 复制构造方法

		public SaloonCar(SaloonCar sc) {
			this.color = sc.getColor();
			this.age = sc.getAge();
			this.gearBox = sc.getGearBox();
		}

		public SaloonCar(String color, int age, GearBox gearBox) {
			this.color = color;
			this.age = age;
			this.gearBox = gearBox;
		}

		public String getColor() {
			return color;
		}

		public void setColor(String color) {
			this.color = color;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

		public GearBox getGearBox() {
			return gearBox;
		}

		public void setGearBox(GearBox gearBox) {
			this.gearBox = gearBox;
		}

		@Override
		public String toString() {
			return "SaloonCar [color=" + color + ", age=" + age + ", gearBox=" + gearBox + "]";
		}

	}

	/**
	 * 变速箱类
	 */
	static class GearBox implements Serializable{
		private static final long serialVersionUID = -8748254132289711557L;
		// 属性
		private String type;// 变速箱类型
		private String manufacturer; // 生产厂商

		public GearBox(String type, String manufacturer) {
			this.type = type;
			this.manufacturer = manufacturer;
		}

		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getManufacturer() {
			return manufacturer;
		}

		public void setManufacturer(String manufacturer) {
			this.manufacturer = manufacturer;
		}

		@Override
		public String toString() {
			return "GearBox [type=" + type + ", manufacturer=" + manufacturer + "]";
		}

	}
}

运行结果

sc1:SaloonCar [color=白色, age=3, gearBox=GearBox [type=6AT, manufacturer=爱信]]
sc2:SaloonCar [color=白色, age=3, gearBox=GearBox [type=6AT, manufacturer=爱信]]
----------------------------------------------------------
sc1:SaloonCar [color=白色, age=3, gearBox=GearBox [type=CVT, manufacturer=爱信]]
sc2:SaloonCar [color=黑色, age=5, gearBox=GearBox [type=6AT, manufacturer=爱信]]

总结

       通过对象深拷贝的两种实现方式不难看出,随着业务场景的复杂化,当一个类中设计的属性比较多的时候,引用层次比较深的时候,采用重写object类的clone方法是不可取的,采取序列化的方式实现对象深拷贝才是较为合适的。此外,通过对象序列化与反序列的方式还可以在系统编译期间检查要复制的对象是否支持序列化,如果不支持,会在编译时抛错,让错误发生在编译期间总比在运行期间要好。

你可能感兴趣的:(java学习之旅)