浅克隆:原始类型为值传递,对象类型仍为引用传递。
深克隆:所有元素或属性均完全复制,与原对象完全脱离,也就是说所有对于新对象的修改都不会反映到原对象中。
这里主要讲深克隆,要克隆序列化的对象,只需将对象序列化输入字节流中,然后再从字节流读出,这样就可以创建一个新的对象了,新对象和母对象不存在引用共享问题,真正实现对像的深克隆。
public class SerialCloneable implements Cloneable, Serializable {
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 (IOException e) {
// TODO Auto-generated catch block
return null;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
需克隆的对象
public class Employee extends SerialCloneable{
private String name;
private double salary;
private Date hireDay;
public Employee(String name, double salary, int year,int month,int day) {
super();
this.name = name;
this.salary = salary;
GregorianCalendar calendar=new GregorianCalendar(year, month-1, day);
this.hireDay=calendar.getTime();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Date getHireDay() {
return hireDay;
}
public void setHireDay(Date hireDay) {
this.hireDay = hireDay;
}
@Override
public String toString() {
return getClass().getName()+" [name=" + name + ", salary=" + salary + ", hireDay=" + hireDay + "]";
}
public void raiseSalary(double byPercent){
double raise=salary*byPercent/100;
salary+=raise;
}
}
测试
public class SerialCloneTest {
public static void main(String[] args) {
Employee harry=new Employee("harry hacker", 20000, 2017,8,9);
Employee harry2=(Employee) harry.clone();
harry.raiseSalary(10);
System.out.println(harry);
System.out.println(harry2);
}
}
可以看出,当母对象的salary值发生改变,并没有影响到子对象,因为使用深克隆他们就不存在引用共享问题