Cloneable:CloneableSupportedException
只有子类实现了Cloneable接口后才能使用Object类提供的clone(),否则会抛出上述异常。
protected native Object clone() throws CloneNotSupportedException;
要想让对象具有拷贝的功能,必须实现Cloneable接口(标识接口,表示此类允许被克隆),并且在类中自定义clone()调用Object类提供的继承权限clone()。
1.浅拷贝
对象值拷贝。对于浅拷贝而言,拷贝出来的对象仍然保留原对象的所有引用。
问题:牵一发而动全身(只要任意一个拷贝对象(或原对象中的引用发生改变,所有对象均会受到影响。)
package com.xunpu.jvm;
//浅拷贝
class Teacher{
private String name;
public Teacher(String name, String job) {
this.name = name;
this.job = job;
}
private String job;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
class Student implements Cloneable{
private String name;
private int age;
private Teacher teacher;
public Student clone(){
Student student=null;
try {
//实现拷贝处理
//产生一个新的student对象,并且拷贝所有的原有属性值
student= (Student) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return student;
}
public Student(String name, int age, Teacher teacher) {
this.name = name;
this.age = age;
this.teacher = teacher;
}
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;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
public class TestClone {
public static void main(String[] args) {
Teacher teacher=new Teacher("张三","java teacher");
Student student=new Student("boss",18,teacher);
Student studentClone=student.clone();
System.out.println(studentClone);
System.out.println(student);
System.out.println(studentClone.getName());
System.out.println(studentClone.getAge());
System.out.println(studentClone.getTeacher().getJob()+":"+studentClone.getTeacher().getName());
System.out.println("===========================");
System.out.println(student.getTeacher()==studentClone.getTeacher());//true
}
}
结果:
浅拷贝原理:
2.深拷贝
深拷贝,拷贝出来的对象产生了所有引用的新的对象。
特点:修改任意一个对象不会对其它对象产生影响。
如何实现深拷贝?
1)包含的其它类继续实现Cloneable接口,并且调用clone()(递归实现clone)。
2)使用序列化。
使用序列化进行深拷贝时,无需再实现Cloneable接口,只需要实现Serializable接口即可。
//深拷贝
//实现方法1):包含的其它类继续实现Cloneable接口,并且调用clone()(递归实现clone)。
package com.xunpu.jvm;
import com.sun.xml.internal.ws.util.xml.NamespaceContextExAdaper;
class Teacher implements Cloneable {
private String name;
public Teacher(String name, String job) {
this.name = name;
this.job = job;
}
public Teacher clone(){
Teacher teacher=null;
try {
teacher= (Teacher) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return teacher;
}
private String job;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
class Student implements Cloneable{
private String name;
private int age;
private Teacher teacher;
public Student clone(){
Student student=null;
try {
//实现拷贝处理
//产生一个新的student对象,并且拷贝所有的原有属性值
student= (Student) super.clone();
student.teacher=this.teacher.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return student;
}
public Student(String name, int age, Teacher teacher) {
this.name = name;
this.age = age;
this.teacher = teacher;
}
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;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
public class TestClone {
public static void main(String[] args) {
Teacher teacher=new Teacher("张三","java teacher");
Student student=new Student("boss",18,teacher);
Student studentClone=student.clone();
System.out.println(studentClone);
System.out.println(student);
System.out.println(studentClone.getName());
System.out.println(studentClone.getAge());
System.out.println(studentClone.getTeacher().getJob()+":"+studentClone.getTeacher().getName());
System.out.println("===========================");
System.out.println(student.getTeacher()==studentClone.getTeacher());//true
}
}
结果:
//深拷贝
//方式二:使用序列化
package com.xunpu.jvm;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import java.io.*;
class Teacher implements Serializable {
private String name;
public Teacher(String name, String job) {
this.name = name;
this.job = job;
}
private String job;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
class Student implements Serializable {
private String name;
private int age;
private Teacher teacher;
public Student cloneObject() throws Exception{
//通过内存进行序列化的读取与写入
//获取内存流
ByteOutputStream bos=new ByteOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(bos);
oos.writeObject(this);
ByteArrayInputStream bis=new ByteArrayInputStream(bos.getBytes());
//获取序列化的两个核心类
ObjectInputStream ois=new ObjectInputStream(bis);
return (Student) ois.readObject();
}
public Student(String name, int age, Teacher teacher) {
this.name = name;
this.age = age;
this.teacher = teacher;
}
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;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
public class TestClone {
public static void main(String[] args) throws Exception {
Teacher teacher=new Teacher("张三","java teacher");
Student student=new Student("boss",18,teacher);
Student studentClone=student.cloneObject();
System.out.println(studentClone);
System.out.println(student);
System.out.println(studentClone.getName());
System.out.println(studentClone.getAge());
System.out.println(studentClone.getTeacher().getJob()+":"+studentClone.getTeacher().getName());
System.out.println("===========================");
System.out.println(student.getTeacher()==studentClone.getTeacher());//true
}
}
结果同上。
深拷贝原理: