将一个对象的引用赋值给另一个对象
1. 直接赋值
A a1 = a2;
a1变化 a2也变化
2.浅复制
复制引用不复制引用的对象
创建新对象,将当前对象的非静态字段赋值到新对象,值类型复制,引用类型,复制引用而不复制引用的对象
public class Resume implements Cloneable {
public Object clone() {
try {
return (Resume) super.clone();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
3.深复制
复制对象本身,复制对象包含的引用指向的对象
public class Student implements Cloneable{
String name;
Resume resume;
public Student(String name, Resume resume) {
this.name = name;
this.resume = resume;
}
public Object clone(){
Student s = null;
try {
s = (Student) super.clone();
} catch (Exception e){
e.printStackTrace();
}
s.resume = (Resume) resume.clone();
return s;
}
}
标签:Resume,对象,clone,resume,复制,public
From: https://www.cnblogs.com/lwx11111/p/17648322.html