1.java拷贝对象有两种方式
a.使用java.lang.Object类中clone方法进行对象的拷贝
b.利用序列化进行对象的拷贝
2.利用Object的clone进行对象拷贝分为两种
a.浅拷贝:仅仅拷贝所考虑的对象,不拷贝引用的对象
b.深拷贝:不仅拷贝所考虑的对象,而且拷贝引用的对象
3.利用Object的clone进行对象拷贝,需要满足三个条件
a.类必须实现java.lang.Cloneable接口(否则拷贝对象时会抛出CloneNotSupportedException异常,不支持拷贝)
b.类必须重写clone方法,并声明为public(否则非子类不能调用clone方法进行拷贝)
c.类中clone方法中必须调用super.clone()
4.利用Object的clone方法进行深拷贝
//实现Cloneable接口
class Student implements Cloneable {
String studentName;
Teacher teacher;
public Student(String studentName, Teacher teacher) {
this.studentName = studentName;
this.teacher = teacher;
}
// 重写Object的clone方法,并在方法中使用super.clone()
@Override
public Object clone() {
// 拷贝Student类
Student student = null;
try {
student = (Student) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return student;
}
}
// 实现Cloneable接口
class Teacher implements Cloneable {
String teacherName;
public Teacher(String teacherName) {
this.teacherName = teacherName;
}
// 重写Object的clone方法,并在方法中使用super.clone()
@Override
public Object clone() {
Teacher teacher = null;
try {
teacher = (Teacher) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return teacher;
}
}
public class CloneTest {
public static void main(String[] args) throws CloneNotSupportedException {
// 创建一个老师
Teacher teacher = new Teacher("老师");
// 创建一个学生,并且引用老师
Student student1 = new Student("学生", teacher);
// 拷贝一个学生
Student student2 = (Student) student1.clone();
// 检查学生是否同一个对象
System.out.println("检查两个学生对象:" + (student1 == student2));
// 检查老师是否同一个对象
System.out.println("检查两个老师对象:" + (student2.teacher== teacher));
}
}
打印结果为:
检查两个学生对象:false
检查两个老师对象:true
说明:只有学生对象被拷贝了,老师对象没有被拷贝,此拷贝方法为浅拷贝
5.利用Object的clone方法进行深拷贝
//实现Cloneable接口
class Student implements Cloneable {
String studentName;
Teacher teacher;
public Student(String studentName, Teacher teacher) {
this.studentName = studentName;
this.teacher = teacher;
}
// 重写Object的clone方法,并在方法中使用super.clone()
@Override
public Object clone() {
// 拷贝Student类
Student student = null;
try {
student = (Student) super.clone();
// 获取Student类中的Teacher类进行进行拷贝
student.teacher =(Teacher) student.teacher.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return student;
}
}
// 实现Cloneable接口
class Teacher implements Cloneable {
String teacherName;
public Teacher(String teacherName) {
this.teacherName = teacherName;
}
// 重写Object的clone方法,并在方法中使用super.clone()
@Override
public Object clone() {
Teacher teacher = null;
try {
teacher = (Teacher) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return teacher;
}
}
public class CloneTest {
public static void main(String[] args) throws CloneNotSupportedException {
// 创建一个老师
Teacher teacher = new Teacher("老师");
// 创建一个学生,并且引用老师
Student student1 = new Student("学生", teacher);
// 拷贝一个学生
Student student2 = (Student) student1.clone();
// 检查学生是否同一个对象
System.out.println("检查两个学生对象:" + (student1 == student2));
// 检查老师是否同一个对象
System.out.println("检查两个老师对象:" + (student2.teacher== teacher));
}
}
打印结果为:
检查两个学生对象:false
检查两个老师对象:false
说明:学生和老师对象都被拷贝了,此拷贝方法为深拷贝
6.利用序列化(Serializable)实现拷贝对象
a.如需对序列化详细了解,请点击传送门
b.使用序列化拷贝对象
class Student implements Serializable {
private static final long serialVersionUID = -5734927474270419167L;
String studentName;
Teacher teacher;
public Student(String studentName, Teacher teacher) {
this.studentName = studentName;
this.teacher = teacher;
}
}
// 实现Serializable接口
class Teacher implements Serializable {
private static final long serialVersionUID = -8357235700226262125L;
String teacherName;
public Teacher(String teacherName) {
this.teacherName = teacherName;
}
}
public class CloneTest {
public static void main(String[] args) throws Exception {
// 创建一个老师
Teacher teacher = new Teacher("老师");
// 创建一个学生,并且引用老师
Student student1 = new Student("学生", teacher);
// 序列化
FileOutputStream fos = new FileOutputStream("Student.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(student1);
// 反序列化
FileInputStream fis = new FileInputStream("Student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Student student2 = (Student) ois.readObject();
// 检查学生是否同一个对象
System.out.println("检查两个学生对象:" + (student1 == student2));
// 检查老师是否同一个对象
System.out.println("检查两个老师对象:" + (student2.teacher == teacher));
oos.close();
ois.close();
}
}
打印结果为:
检查两个学生对象:false
检查两个老师对象:false
说明:使用序列化学生和老师对象都被拷贝了,此拷贝方法为深拷贝