1.java里的clone分为:
A:浅复制(浅克隆): 浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。
B:深复制(深克隆):深复制把要复制的对象所引用的对象都复制了一遍。
Java中对象的克隆,为了获取对象的一份拷贝,我们可以利用Object类的clone()方法。必须要遵循下面三点
1.在派生类中覆盖基类的clone()方法,并声明为public【Object类中的clone()方法为protected的】。
2.在派生类的clone()方法中,调用super.clone()。
3.在派生类中实现Cloneable接口。
Object类里的clone方法是浅复制(浅克隆)
1 浅复制 (浅克隆)的例子如下:
20
zhangsan
~~~~~~~~~~~~~~~~~~~~~~
40
Teacher zhang
~~~~~~~~~~~~~~~~~~~~~~
50
Teacher Li
2 .深复制(深Clone)例子:
20
zhangsan
~~~~~~~~~~~~~~~~~~~~~~
40
Teacher zhang
~~~~~~~~~~~~~~~~~~~~~~
40
Teacher zhang
3.利用序列化来做深复制,把对象写到流里的过程是序列化(Serilization)过程,而把对象从流中读出来的过程则叫做反序列化(Deserialization)过程。应当指出的是,写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面。,利用这个特性,可以做深拷贝
package com.test3;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
A:浅复制(浅克隆): 浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。
B:深复制(深克隆):深复制把要复制的对象所引用的对象都复制了一遍。
Java中对象的克隆,为了获取对象的一份拷贝,我们可以利用Object类的clone()方法。必须要遵循下面三点
1.在派生类中覆盖基类的clone()方法,并声明为public【Object类中的clone()方法为protected的】。
2.在派生类的clone()方法中,调用super.clone()。
3.在派生类中实现Cloneable接口。
Object类里的clone方法是浅复制(浅克隆)
1 浅复制 (浅克隆)的例子如下:
package com.test;
//浅复制(浅克隆): 浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。
//深复制(深克隆):深复制把要复制的对象所引用的对象都复制了一遍。
//
//Java中对象的克隆,为了获取对象的一份拷贝,我们可以利用Object类的clone()方法。必须要遵循下面三点
//1.在派生类中覆盖基类的clone()方法,并声明为public【Object类中的clone()方法为protected的】。
//2.在派生类的clone()方法中,调用super.clone()。
//3.在派生类中实现Cloneable接口。
//Object类里的clone方法是浅复制(浅克隆)
public class CloneTest {
public static void main(String[] args) throws Exception{
//teacher对象将被clone出来的Student对象共享.
Teacher teacher = new Teacher();
teacher.setAge(40);
teacher.setName("Teacher zhang");
Student student1 = new Student();
student1.setAge(20);
student1.setName("zhangsan");
student1.setTeacher(teacher);
//复制出来一个对象student2
Student student2 = (Student)student1.clone();
System.out.println(student2.getAge());
System.out.println(student2.getName());
System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(student1.getTeacher().getAge());
System.out.println(student1.getTeacher().getName());
//修改student2的引用对象
student2.getTeacher().setAge(50);
student2.getTeacher().setName("Teacher Li");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(student1.getTeacher().getAge());
System.out.println(student1.getTeacher().getName());
}
}
class Teacher {
public int age;
public String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Student implements Cloneable{
public int age ;
public String name;
public Teacher teacher;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
输出结果为:
20
zhangsan
~~~~~~~~~~~~~~~~~~~~~~
40
Teacher zhang
~~~~~~~~~~~~~~~~~~~~~~
50
Teacher Li
2 .深复制(深Clone)例子:
package com.test1;
//深clone
public class DeepCloneTest {
public static void main(String[] args) throws Exception{
//teacher对象将不被clone出来的Student对象共享.
Teacher teacher = new Teacher();
teacher.setAge(40);
teacher.setName("Teacher zhang");
Student student1 = new Student();
student1.setAge(20);
student1.setName("zhangsan");
student1.setTeacher(teacher);
//复制出来一个对象student2
Student student2 = (Student)student1.clone();
System.out.println(student2.getAge());
System.out.println(student2.getName());
System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(student1.getTeacher().getAge());
System.out.println(student1.getTeacher().getName());
//修改student2的引用对象
student2.getTeacher().setAge(50);
student2.getTeacher().setName("Teacher Li");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(student1.getTeacher().getAge());
System.out.println(student1.getTeacher().getName());
}
}
class Teacher implements Cloneable{
public int age;
public String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Student implements Cloneable{
public int age ;
public String name;
public Teacher teacher;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public Object clone() throws CloneNotSupportedException {
Student student = (Student)super.clone();
//将引用的对象teacher也clone下
student.setTeacher((Teacher)(student.getTeacher().clone()));
return student;
}
}
输出结果为:
20
zhangsan
~~~~~~~~~~~~~~~~~~~~~~
40
Teacher zhang
~~~~~~~~~~~~~~~~~~~~~~
40
Teacher zhang
3.利用序列化来做深复制,把对象写到流里的过程是序列化(Serilization)过程,而把对象从流中读出来的过程则叫做反序列化(Deserialization)过程。应当指出的是,写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面。,利用这个特性,可以做深拷贝
package com.test3;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;