Java中對象的創建
複製對象 or 複製引用
- Person p = new Person(23, “zhang”);
- Person p1 = p;
- System.out.println(p);
- System.out.println(p1);
Person p = new Person(23, "zhang");
Person p1 = p;
System.out.println(p);
System.out.println(p1);
當Person p1 = p;執行之後, 是創建了一個新的對象嗎? 首先看打印結果:
com.pansoft.zhangjg.testclone.Person@2f9ee1ac
- Person p = new Person(23, “zhang”);
- Person p1 = (Person) p.clone();
- System.out.println(p);
- System.out.println(p1);
Person p = new Person(23, "zhang");
Person p1 = (Person) p.clone();
System.out.println(p);
System.out.println(p1);
從打印結果可以看出,兩個對象的地址是不同的,也就是說創建了新的對象, 而不是把原對象的地址賦給了一個新的引用變量:
com.pansoft.zhangjg.testclone.Person@67f1fba0
深拷貝 or 淺拷貝
- public class Person implements Cloneable{
- private int age ;
- private String name;
- public Person(int age, String name) {
- this.age = age;
- this.name = name;
- }
- public Person() {}
- public int getAge() {
- return age;
- }
- public String getName() {
- return name;
- }
- @Override
- protected Object clone() throws CloneNotSupportedException {
- return (Person)super.clone();
- }
- }
public class Person implements Cloneable{
private int age ;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public Person() {}
public int getAge() {
return age;
}
public String getName() {
return name;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return (Person)super.clone();
}
}
- Person p = new Person(23, “zhang”);
- Person p1 = (Person) p.clone();
- String result = p.getName() == p1.getName()
- ? ”clone是淺拷貝的” : “clone是深拷貝的”;
- System.out.println(result);
Person p = new Person(23, "zhang");
Person p1 = (Person) p.clone();
String result = p.getName() == p1.getName()
? "clone是淺拷貝的" : "clone是深拷貝的";
System.out.println(result);
打印結果爲:
clone是淺拷貝的
覆蓋Object中的clone方法, 實現深拷貝
- static class Body implements Cloneable{
- public Head head;
- public Body() {}
- public Body(Head head) {this.head = head;}
- @Override
- protected Object clone() throws CloneNotSupportedException {
- return super.clone();
- }
- }
- static class Head /*implements Cloneable*/{
- public Face face;
- public Head() {}
- public Head(Face face){this.face = face;}
- }
- public static void main(String[] args) throws CloneNotSupportedException {
- Body body = new Body(new Head());
- Body body1 = (Body) body.clone();
- System.out.println(”body == body1 : ” + (body == body1) );
- System.out.println(”body.head == body1.head : ” + (body.head == body1.head));
- }
static class Body implements Cloneable{
public Head head;
public Body() {}
public Body(Head head) {this.head = head;}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
static class Head /*implements Cloneable*/{
public Face face;
public Head() {}
public Head(Face face){this.face = face;}
}
public static void main(String[] args) throws CloneNotSupportedException {
Body body = new Body(new Head());
Body body1 = (Body) body.clone();
System.out.println("body == body1 : " + (body == body1) );
System.out.println("body.head == body1.head : " + (body.head == body1.head));
}
在以上代碼中, 有兩個主要的類, 分別爲Body和Face, 在Body類中, 組合了一個Face對象。當對Body對象進行clone時, 它組合的Face對象只進行淺拷貝。打印結果可以驗證該結論:
body.head == body1.head : true
- 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();
- }
- }
- public static void main(String[] args) throws CloneNotSupportedException {
- Body body = new Body(new Head());
- Body body1 = (Body) body.clone();
- System.out.println(”body == body1 : ” + (body == body1) );
- System.out.println(”body.head == body1.head : ” + (body.head == body1.head));
- }
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();
}
}
public static void main(String[] args) throws CloneNotSupportedException {
Body body = new Body(new Head());
Body body1 = (Body) body.clone();
System.out.println("body == body1 : " + (body == body1) );
System.out.println("body.head == body1.head : " + (body.head == body1.head));
}
打印結果爲:
body.head == body1.head : false
真的是深拷貝嗎
- 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));
- }
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.head == body1.head : false
body.head.face == body1.head.face : true
如何進行徹底的深拷貝
- 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();
- Head newHead = (Head) super.clone();
- newHead.face = (Face) this.face.clone();
- return newHead;
- }
- }
- static class Face implements Cloneable{
- @Override
- protected Object clone() throws CloneNotSupportedException {
- return super.clone();
- }
- }
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();
Head newHead = (Head) super.clone();
newHead.face = (Face) this.face.clone();
return newHead;
}
}
static class Face implements Cloneable{
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
再次運行上面的示例,得到的運行結果如下:
body.head == body1.head : false
body.head.face == body1.head.face : false