大家好,又见面了,我是全栈君。
转载自:http://blog.csdn.net/zhangjg_blog/article/details/18369201
Java中对象的创建
复制对象 or 复制引用
- 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);
从打印结果可以看出,两个对象的地址是不同的,也就是说创建了新的对象, 而不是把原对象的地址赋给了一个新的引用变量:
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();
- }
- }
- Person p = new Person(23, “zhang”);
- Person p1 = (Person) p.clone();
- String result = p.getName() == p1.getName()
- ? “clone是浅拷贝的” : “clone是深拷贝的”;
- System.out.println(result);
打印结果为:
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));
- }
在以上代码中, 有两个主要的类, 分别为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));
- }
打印结果为:
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));
- }
打印结果为:
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();
- }
- }
再次运行上面的示例,得到的运行结果如下:
body.head == body1.head : false
body.head.face == body1.head.face : false
写在最后
转载于:https://www.cnblogs.com/harrogath/p/6464100.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/108649.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...