浅拷贝(Shallow Copy):仅拷贝对象的非对象属性(基本类型属性)、不可变对象属性,但是不拷贝对象的对象属性(不含不可变对象) ,即为浅拷贝。
public class Subject {
private String name;
public Subject(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "[Subject: " + this.hashCode() + ",name:" + name + "]";
}
}
public class Student implements Cloneable {
/**
* 引用类型
*/
private Subject subject;
/**
* 基础数据类型
*/
private String name;
/**
* 基础数据类型
*/
private int age;
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
/**
* 重写clone()方法
*
* @return
*/
@Override
public Object clone() {
//浅拷贝
try {
// 直接调用父类的clone()方法
return super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
@Override
public String toString() {
return "[Student: " + this.hashCode() + ",subject:" + subject + ",name:" + name + ",age:" + age + "]";
}
}
public class ShallowCopy {
public static void main(String[] args) {
// 语文课
Subject subject = new Subject("语文");
// 学生张三
Student studentA = new Student();
studentA.setSubject(subject);
studentA.setName("张三");
studentA.setAge(20);
// 拷贝一个对象,名字叫李四
Student studentB = (Student) studentA.clone();
studentB.setName("李四");
studentB.setAge(18);
// 主修课程
Subject subjectB = studentB.getSubject();
subjectB.setName("历史");
// 输出两个对象
System.out.println("studentA:" + studentA.toString());
System.out.println("studentB:" + studentB.toString());
}
}
执行以上代码,执行结果如下。
深拷贝(Deep Copy):对基本数据类型进行值传递,对引用数据类型,创建一个新的对象,并复制其内容,此为深拷贝。
public class Subject implements Cloneable {
private String name;
public Subject(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "[Subject: " + this.hashCode() + ",name:" + name + "]";
}
@Override
protected Object clone() throws CloneNotSupportedException {
//Subject 如果也有引用类型的成员属性,也应该和 Student 类一样实现
return super.clone();
}
}
public class Student implements Cloneable {
/**
* 引用类型
*/
private Subject subject;
/**
* 引用类型
*/
private String name;
/**
* 基础数据类型
*/
private int age;
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
/**
* 重写clone()方法
*
* @return
*/
@Override
public Object clone() {
//浅拷贝
try {
// 直接调用父类的clone()方法
Student student = (Student) super.clone();
student.subject = (Subject) subject.clone();
return student;
} catch (CloneNotSupportedException e) {
return null;
}
}
@Override
public String toString() {
return "[Student: " + this.hashCode() + ",subject:" + subject + ",name:" + name + ",age:" + age + "]";
}
}
public class ShallowCopy {
public static void main(String[] args) {
// 语文课
Subject subject = new Subject("语文");
// 学生张三
Student studentA = new Student();
studentA.setSubject(subject);
studentA.setName("张三");
studentA.setAge(20);
// 拷贝一个对象,名字叫李四
Student studentB = (Student) studentA.clone();
studentB.setName("李四");
studentB.setAge(18);
// 主修课程
Subject subjectB = studentB.getSubject();
subjectB.setName("历史");
// 输出两个对象
System.out.println("studentA:" + studentA.toString());
System.out.println("studentB:" + studentB.toString());
}
}
执行以上代码,执行结果如下。
studentA:[Student: 1846274136,subject:[Subject: 1639705018,name:语文],name:张三,age:20]
studentB:[Student: 1627674070,subject:[Subject: 1360875712,name:历史],name:李四,age:18]