Java中深拷贝与浅拷贝

时间:2022-05-14 19:55:34
首先我们看看浅拷贝和深拷贝的定义

    浅拷贝:只复制一个对象,对象内部存在的指向其他对象数组或者引用则不复制

    深拷贝:对象,对象内部的引用均复制

    为了更好的理解它们的区别我们假设有一个对象A,它包含有2对象对象A1和对象A2

Java中深拷贝与浅拷贝 

    对象A进行浅拷贝后,得到对象B但是对象A1和A2并没有被拷贝(此时对对象B进行修改会影响到对象A中(对象A1、对象A2的值))


Java中深拷贝与浅拷贝

   示例代码:

package CoreJava;

import java.util.Date;
import java.util.GregorianCalendar;

/**
* 浅拷贝
* @author xqh
*
*/
public class CloneTest1 {
public static void main(String[] args) {
Employee1 original = new Employee1("张三", 5000);
original.setHireDay(2011, 8, 29);

Employee1 copy = (Employee1)original.clone();
copy.raiseSalary(10);
copy.setHireDay(2011, 9, 11);
System.out.println("original = " + original);
System.out.println("copy = " + copy);
}
}
class Employee1 implements Cloneable{
private String name;
private double salary;
private Date hireDay;

public Employee1(String name, double salary) {
this.name = name;
this.salary = salary;
hireDay = new Date();
}

public void setHireDay(int year, int month, int day) {
Date newHireDay = new GregorianCalendar(year, month - 1, day).getTime();
hireDay.setTime(newHireDay.getTime());
}

public void raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
}

public Employee1 clone() {
try {
return (Employee1) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}

public String toString() {
return "Employee[name" + name + ", salary=" + salary + ", hireDay=" + hireDay +"]";
}
}

结果:

original = Employee[name张三, salary=5000.0, hireDay=Sun Sep 11 00:00:00 CST 2011]
copy = Employee[name张三, salary=5500.0, hireDay=Sun Sep 11 00:00:00 CST 2011]
由此结果可看出:copy中修改hireDay的值影响到original对象中的值

  对象A进行深拷贝,得到对象B的同时A1和A2连同它们的引用也被拷贝(此时对对象B进行修改不会影响到对象A中(对象A1、对象A2的值))

Java中深拷贝与浅拷贝


示例代码:

package CoreJava;

import java.util.Date;
import java.util.GregorianCalendar;

/**
* 深拷贝
* @author xqh
*
*/
public class CloneTest {
public static void main(String[] args) {
Employee original = new Employee("张三", 5000);
original.setHireDay(2011, 8, 29);

Employee copy = original.clone();
copy.raiseSalary(10);
copy.setHireDay(2011, 9, 11);
System.out.println("original = " + original);
System.out.println("copy = " + copy);
}
}

class Employee implements Cloneable {
private String name;
private double salary;
private Date hireDay;

public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
hireDay = new Date();
}

public Employee clone() {
Employee cloned = null;
try {
cloned = (Employee)super.clone();
cloned.hireDay = (Date)hireDay.clone(); // 实现对hireDay深拷贝
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return cloned;
}

public void setHireDay(int year, int month, int day) {
Date newHireDay = new GregorianCalendar(year, month - 1, day).getTime();
hireDay.setTime(newHireDay.getTime());
}

public void raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
}

public String toString() {
return "Employee[name" + name + ", salary=" + salary + ", hireDay=" + hireDay +"]";
}
}

结果:

original = Employee[name张三, salary=5000.0, hireDay=Mon Aug 29 00:00:00 CST 2011]
copy = Employee[name张三, salary=5500.0, hireDay=Sun Sep 11 00:00:00 CST 2011]
由此结果可看出:copy中修改hireDay的值没有影响到original对象中的值


深拷贝的另一种实现方法:

package CoreJava;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.GregorianCalendar;

/**
* 序列化机制有一种很有趣的用法 :它提供了一种克隆对象的简便途径,只要对应的类是可
* 序列化的即可。其做法很简单:直接将对象序列化到输出流中,然后将其读回。这样产生
* 的新对象是对现有对象的一个深拷贝(deep copy)。
* 尽管这个方法很灵巧,但它通常比显式地构建新对象并复制或克隆数据域的克隆方法慢得多。
*
* @author xqh
*
*/
public class IO_SerialCloneTest {
public static void main(String[] args) {
EmployeeClone harry = new EmployeeClone("Harry Hacker", 3500, 1989, 10, 1);
//clone harry
EmployeeClone harry2 = (EmployeeClone)harry.clone();

harry.raiseSalary(10);

System.out.println(harry);
System.out.println(harry2);
}
}

class SerialCloneable implements Cloneable, Serializable {

private static final long serialVersionUID = 1L;

public Object clone() {
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(this);
out.close();

ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream in = new ObjectInputStream(bin);
Object ret = in.readObject();
in.close();
return ret;
} catch (Exception e) {
return null;
}
}
}

class EmployeeClone extends SerialCloneable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private double salary;
private Date hireDay;

EmployeeClone() {}

public EmployeeClone(String name, double salary, int year, int month, int day) {
this.name = name;
this.salary = salary;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
this.hireDay = calendar.getTime();
}

public Date getHireDay() {
return hireDay;
}

public String getName() {
return name;
}

public double getSalary() {
return salary;
}

public void raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
}

public void setHireDay(Date hireDay) {
this.hireDay = hireDay;
}

public void setName(String name) {
this.name = name;
}

public void setSalary(double salary) {
this.salary = salary;
}

public String toString() {
return getClass().getName() + "[ name " + name + ", salary = " + salary + ", hireDay = " + hireDay +"]";
}
}

程序运行结果:

CoreJava.EmployeeClone[ name Harry Hacker, salary = 3850.0, hireDay = Sun Oct 01 00:00:00 CST 1989]
CoreJava.EmployeeClone[ name Harry Hacker, salary = 3500.0, hireDay = Sun Oct 01 00:00:00 CST 1989]


参考:http://www.duote.com/tech/5/12055.html