1.浅克隆
浅克隆,只是克隆了基本的数据类型,对于引用类型的对象只是复制了地址。在克隆后的新对象中,改变引用对象的属性,也会影响旧的对象的引用属性
2.深克隆
在引用对象中也做了clone的嵌套,复制后的对象与原对象之间完全不会影响。
使用clone实现的深克隆其实是浅克隆中嵌套了浅克隆,与toString方法类似
一、浅克隆实现
1.创建Address 实体类
package cloning;
import java.io.Serializable;
public class Address implements Cloneable, Serializable {
private String country;
private String province;
private String city;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address [country=" + country + ", province=" + province
+ ", city=" + city + "]";
}
public Address(String country, String province, String city) {
super();
this.country = country;
this.province = province;
this.city = city;
}
}
2.创建Customer 实体类
package cloning;
import java.io.*;
public class Customer implements Cloneable, Serializable {
public int ID;
public int age;
public Address address;
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Customer(int iD, int age, Address address) {
super();
ID = iD;
this.age = age;
this.address = address;
}
@Override
public String toString() {
return "Customer [ID=" + ID + ", age=" + age + ", address=" + address
+ "]";
}
@Override
public Customer clone() throws CloneNotSupportedException {
return (Customer) super.clone();
}
}
3、测试浅克隆
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("CH", "SD", "QD");
Customer customer1 = new Customer(1, 23, address);
Customer customer2 = customer1.clone();
System.out.println("customer1:" + customer1.toString());
System.out.println("customer2:" + customer2.toString());
//修改引用型的对象
customer2.getAddress().setCity("JN");
customer2.setID(2);
System.out.println("customer1:" + customer1.toString());
System.out.println("customer2:" + customer2.toString());
}
4.实现结果
customer1:Customer [ID=1, age=23, address=Address [country=CH, province=SD, city=QD]]
customer2:Customer [ID=1, age=23, address=Address [country=CH, province=SD, city=QD]]
customer1:Customer [ID=1, age=23, address=Address [country=CH, province=SD, city=JN]]
customer2:Customer [ID=2, age=23, address=Address [country=CH, province=SD, city=JN]]
二、深度克隆之克隆嵌套
1.在Address实体类中重写克隆方法
@Override
public Address clone() throws CloneNotSupportedException {
return (Address) super.clone();
}
2.重写Customer类的克隆方法
@Override
public Customer clone() throws CloneNotSupportedException {
Customer customer = (Customer) super.clone();
customer.address = address.clone();
return customer;
}
3.测试深克隆
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("CH", "SD", "QD");
Customer customer1 = new Customer(1, 23, address);
Customer customer2 = customer1.clone();
System.out.println("customer1:" + customer1.toString());
System.out.println("customer2:" + customer2.toString());
//修改引用型的对象
customer2.getAddress().setCity("JN");
customer2.setID(2);
System.out.println("customer1:" + customer1.toString());
System.out.println("customer2:" + customer2.toString());
}
4、深克隆调试结果
customer1:Customer [ID=1, age=23, address=Address [country=CH, province=SD, city=QD]]
customer2:Customer [ID=1, age=23, address=Address [country=CH, province=SD, city=QD]]
customer1:Customer [ID=1, age=23, address=Address [country=CH, province=SD, city=QD]]
customer2:Customer [ID=2, age=23, address=Address [country=CH, province=SD, city=JN]]
三、使用序列化也能完成深复制的功能
1.使用序列化程序
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address("CH", "SD", "QD");
Customer customer1 = new Customer(1, 23, address);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(byteOut);
out.writeObject(customer1);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
Customer customer2 = (Customer)in.readObject();
System.out.println("customer1:"+customer1.toString());
System.out.println("customer2:"+customer2.toString());
//修改引用型的对象
customer2.getAddress().setCity("JN");
customer2.setID(2);
System.out.println("customer1:"+customer1.toString());
System.out.println("customer2:"+customer2.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
2.测试结果
customer1:Customer [ID=1, age=23, address=Address [country=CH, province=SD, city=QD]]
customer2:Customer [ID=1, age=23, address=Address [country=CH, province=SD, city=QD]]
customer1:Customer [ID=1, age=23, address=Address [country=CH, province=SD, city=QD]]
customer2:Customer [ID=2, age=23, address=Address [country=CH, province=SD, city=JN]]