Java 深拷贝(深复制或深克隆)和浅拷贝(浅复制或浅克隆)
浅复制:被复制对象的所有变量都含有与原来对象相同的值,而所有其他对象的引用仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。
深复制:被复制对象的所有变量都含有与原来对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制的新对象,而不再是原有的那些被引用的对象。换言之,深复制把复制的对象所引用的对象都复制了一遍。
克隆的实现方式:
1. 类要实现Clonable接口
2. 重写Object的clone方法
3. 对象调用clone方法
浅复制
案例(编写大象类)
//实现Cloneable接口
public class Elephant implements Serializable,Cloneable {
private String name;
private String age;
private String sex;
private Country country =new Country();
//重写Object的clone方法
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public Elephant(String name, String age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
@Override
public String toString() {
return "Elephant{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", sex='" + sex + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
测试
public class CloneTest {
public static void main(String[] args) throws CloneNotSupportedException {
Elephant elephant = new Elephant("小红象", "20", "男");
//克隆大象
Elephant elephant1 = (Elephant) elephant.clone();
System.out.println(elephant);
System.out.println(elephant1);
System.out.println(elephant==elephant1);
}
}
但是浅复制会存在一个问题,当我们给其中一头大象修改地区时,另一个大象地区也会同时进行改变,而大象的基本数据不会改变
添加地区类
public class Country implements Serializable {
private String name="亚洲";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试类
public class CloneTest {
public static void main(String[] args) throws CloneNotSupportedException {
Elephant elephant = new Elephant("小红象", "20", "男");
//克隆大象
Elephant elephant1 = (Elephant) elephant.clone();
System.out.println(elephant);
System.out.println(elephant1);
System.out.println(elephant==elephant1);
//将原来的大象的年龄改为10岁
elephant.setAge("10");
//将克隆出来的大象的地区改为干果
elephant1.getCountry().setName("干果");
System.out.println(elephant+elephant.getCountry().getName());
System.out.println(elephant1+elephant1.getCountry().getName());
System.out.println(elephant==elephant1);
}
}
运行结果
总结:基本数据类型和String完全赋值的值,引用自定义类型赋值的是内存地址
解决上述问题就需要采用深复制
深复制
1.引用类型的属性一个个创建,然后赋值
//实现Cloneable接口
public class Elephant implements Serializable,Cloneable {
private String name;
private String age;
private String sex;
private Country country =new Country();
@Override
protected Object clone() throws CloneNotSupportedException {
Elephant clone = (Elephant) super.clone();
Country country = new Country();
country.setName(this.country.getName());
clone.setCountry(country);
return clone;
}
public Elephant(String name, String age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
@Override
public String toString() {
return "Elephant{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", sex='" + sex + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
运行结果
属性一个个进行创建和赋值代码比较繁琐,工作量也十分的大,所有一般采用序列化和反序列化
2.序列化和反序列化