Clone()
clone()
方法,可以申请一个和被调用对象一样的空间
然后,将被调用对象的成员变量,赋到新申请的空间中
只是,这些成员变量,如果也是类的话,执行的就是浅拷贝
比如下面这样:
public class MyCloneTest2 {
public static void main(String[] args) throws CloneNotSupportedException {
Face2 face = new Face2(new Teeth2(4,4));
Face2 face1 = (Face2) face.clone();
System.out.println("调用 clone 方法的 face 对象是否地址相等:"+(face==face1));
System.out.println("两个 face 对象的成员变量 Teeth 是否地址相等:"+(face.getTeeth2()==face1.getTeeth2()));
System.out.println(face);
System.out.println(face1);
}
}
class Face2 implements Cloneable {
private Teeth2 teeth;
public Face2(Teeth2 teeth) {
this.teeth = teeth;
}
public Face2() {
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public Teeth2 getTeeth2() {
return teeth;
}
public void setTeeth2(Teeth2 teeth) {
this.teeth = teeth;
}
@Override
public String toString() {
return "Face2{" +
"teeth=" + teeth +
'}';
}
}
class Teeth2 implements Cloneable {
private int intNum;
private Integer IntegerNum;
public Teeth2(int intNum, Integer integerNum) {
this.intNum = intNum;
IntegerNum = integerNum;
}
public Teeth2() {
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public int getIntNum() {
return intNum;
}
public void setIntNum(int intNum) {
this.intNum = intNum;
}
public Integer getIntegerNum() {
return IntegerNum;
}
public void setIntegerNum(Integer integerNum) {
IntegerNum = integerNum;
}
@Override
public String toString() {
return "Teeth2{" +
"intNum=" + intNum +
", IntegerNum=" + IntegerNum +
'}';
}
}
结果如下:
如果希望实现完全的深拷贝,那成员变量也需要实现Cloneable
接口,并重写 clone 方法
:
public class MyCloneTest {
public static void main(String[] args) throws CloneNotSupportedException {
Face face = new Face(new Teeth(4,4));
Face face1 = (Face) face.clone();
System.out.println(face==face1);
System.out.println(face.getTeeth()==face1.getTeeth());
System.out.println(face.getTeeth().getIntegerNum()==face1.getTeeth().getIntegerNum());
System.out.println(face);
System.out.println(face1);
}
}
class Face implements Cloneable {
private Teeth teeth;
public Face(Teeth teeth) {
this.teeth = teeth;
}
public Face() {
}
@Override
protected Object clone() throws CloneNotSupportedException {
Face face = (Face) super.clone();
face.setTeeth((Teeth) this.teeth.clone());
return face;
}
public Teeth getTeeth() {
return teeth;
}
public void setTeeth(Teeth teeth) {
this.teeth = teeth;
}
@Override
public String toString() {
return "Face{" +
"teeth=" + teeth +
'}';
}
}
class Teeth implements Cloneable {
private int intNum;
private Integer IntegerNum;
public Teeth(int intNum, Integer integerNum) {
this.intNum = intNum;
IntegerNum = integerNum;
}
public Teeth() {
}
@Override
protected Object clone() throws CloneNotSupportedException {
Teeth teeth = (Teeth) super.clone();
teeth.setIntegerNum(new Integer(this.IntegerNum));
return teeth;
}
public int getIntNum() {
return intNum;
}
public void setIntNum(int intNum) {
this.intNum = intNum;
}
public Integer getIntegerNum() {
return IntegerNum;
}
public void setIntegerNum(Integer integerNum) {
IntegerNum = integerNum;
}
@Override
public String toString() {
return "Teeth{" +
"intNum=" + intNum +
", IntegerNum=" + IntegerNum +
'}';
}
}
结果如下: