浅克隆:对于对象的基本数据类型的变量,将值复制一份传递给新对象,是两份数据,对于引用类型变量,会将引用值也就是对象的地址复制一份传递给新对象,是同一份数据。
深克隆:对于基本数据类型的变量,将值复制一份传递给新对象,是两份数据,引用类型会将引用的对象复制一份,然后将新的对象地址传递给新对象。
浅拷贝:
直接上代码:根据地址可知引用类型并未复制,仍然是同一个对象。
public class User implements Cloneable{
private String name;
private int age;
private Student student;
public User(String name, int age, Student student) {
this.name=name;
this.age=age;
this.student = student;
}
public String getName() {
return name;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public static void main(String[] args) throws Exception{
Student student = new Student();
User user1 = new User("xiaoming", 12, student);
User user2 = (User)user1.clone();
System.out.println(student);
System.out.println(user2.getStudent());
System.out.println(user1);
System.out.println(user2);
}
}
class Student {
}
结果:
Student@6d06d69c
Student@6d06d69c
User@7852e922
User@4e25154f
深拷贝:
1.重写clone方法,手动将引用类型复制。结果显示 student引用的是两个对象。
public class User implements Cloneable{
private String name;
private int age;
private Student student;
public User(String name, int age, Student student) {
this.name=name;
this.age=age;
this.student = student;
}
public String getName() {
return name;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
User user = (User)super.clone();
Student student = (Student) user.getStudent().clone();
user.setStudent(student);
return user;
}
public static void main(String[] args) throws Exception{
Student student = new Student();
User user1 = new User("xiaoming", 12, student);
User user2 = (User)user1.clone();
System.out.println(student);
System.out.println(user2.getStudent());
System.out.println(user1);
System.out.println(user2);
}
}
class Student implements Cloneable{
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
结果:
Student@6d06d69c
Student@7852e922
User@4e25154f
User@70dea4e
2.序列化实现深拷贝
public class User implements Serializable{
private static final long serialVersionUID = 5661776984610831718L;
private String name;
private int age;
private Student student;
public User(String name, int age, Student student) {
this.name=name;
this.age=age;
this.student = student;
}
public String getName() {
return name;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static void main(String[] args) throws Exception{
Student student = new Student();
User user1 = new User("xiaoming", 12, student);
FileOutputStream outputStream = new FileOutputStream(new File("d://User.txt"));
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(user1);
FileInputStream inputStream = new FileInputStream(new File("d://User.txt"));
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
User user2 = (User)objectInputStream.readObject();
System.out.println(user1);
System.out.println(user2);
System.out.println(user1.student);
System.out.println(user2.student);
}
}
class Student implements Serializable{
private static final long serialVersionUID = 3218462002829068131L;
}
结果:
User@42a57993
User@4554617c
Student@4aa298b7
Student@74a14482