将一个对象的引用复制给另外一个对象,一共有三种方式。第一种方式是直接赋值,第二种方式是浅拷贝,第三种是深拷贝。
直接赋值复制
直接赋值。在 Java 中, A a1 = a2,我们需要理解的是这实际上复制的是引用,也就是说 a1 和 a2 指向的是同一个对象。因此,当 a1 变化的时候, a2 里面的成员变量也会跟着变化。
//例如
private static void copyReferenceObject(){
Person p = new Person(23, "zhang");
Person p1 = p;
System.out.println(p);
System.out.println(p1);
}
浅复制(复制引用但不复制引用的对象)
创建一个新对象,然后将当前对象的非静态字段复制到该新对象, 如果字段是基本数据类型的,那么对该字段执行复制;如果该字段是引用类型的话,则复制引用但不复制引用的对象。因此,原始对象及其副本引用同一个对象。
实现对象拷贝的类,必须实现Cloneable接口,并覆写clone()方法。
class Resume implements Cloneable{
public Object clone() {
try {
return (Resume)super.clone();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
//例如
package com.yaolong.clone;
public class Person implements Cloneable{
//private Integer age;
private int age;//阿里规范中规定pojo类中的属性强制使用包装类型,这里只是测试private String name;
public Person(Integer age, String name) {
super();
this.age = age;
this.name = name;
}public Integer getAge() {
return age;
}public void setAge(Integer age) {
this.age = age;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}@Override
public String toString() {
return super.toString();
}@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
//对象拷贝
private static void copyRealObject() throws CloneNotSupportedException{
Person p = new Person(23, "zhang");
Person p1 = (Person) p.clone();
System.out.println(p);
System.out.println(p1);System.out.println("pName:"+p.getName().hashCode());
System.out.println("p1Name:"+p1.getName().hashCode());
}
这里打印的结果:
com.yaolong.clone.Person@28084850
com.yaolong.clone.Person@37c390b8
pName: 115864556
p1Name:115864556
可以看出,二者的对象地址不一样,因此实现了拷贝。并且对象中的引用数据类型只是拷贝了引用
深复制(复制对象和其应用对象)
深拷贝不仅复制对象本身,而且复制对象包含的引用指向的所有对象
class Student implements Cloneable {
String name;
int age;
Professor p;
Student(String name, int age, Professor p) {
this.name = name;
this.age = age;
this.p = p;
}
public Object clone() {
Student o = null;
try {
o = (Student) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println(e.toString());
}
o.p = (Professor) p.clone();
return o;
}
}
//例如
static class Body implements Cloneable{
public Head head;
public Body() {}
public Body(Head head) {this.head = head;}@Override
protected Object clone() throws CloneNotSupportedException {
Body newBody = (Body) super.clone();
newBody.head = (Head) head.clone();
return newBody;
}}
static class Head implements Cloneable{
public Face face;public Head() {}
public Head(Face face){this.face = face;}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}static class Face{}
public static void main(String[] args) throws CloneNotSupportedException {
Body body = new Body(new Head(new Face()));
Body body1 = (Body) body.clone();
System.out.println("body == body1 : " + (body == body1) );
System.out.println("body.head == body1.head : " + (body.head == body1.head));
System.out.println("body.head.face == body1.head.face : " + (body.head.face == body1.head.face));
}
打印结果为:
body == body1 : false
body.head == body1.head : false
body.head.face == body1.head.face : true
序列化(深复制的一种实现)
在 Java 语言里深复制一个对象,常常可以先使对象实现 Serializable 接口,然后把对象(实际上只是对象的一个拷贝)写到一个流里,再从流里读出来,便可以重建对象。