Home >Java >javaTutorial >A brief analysis of Java cloning (code example)

A brief analysis of Java cloning (code example)

不言
不言forward
2019-03-12 15:36:412553browse

This article brings you a brief analysis (code example) of Java cloning. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What is depth? Just a degree.

Clone: ​​Make a copy of the

The cloned class implements the Cloneable interface, overrides the clone() method, and returns to the clone() call of the parent class.

public class Person implements Cloneable{
    @Override
    protected Object clone() throws CloneNotSupportedException { 
        return super.clone();
    }
    //...省略其余代码
}

Then, when cloning, just call the clone() we just covered.

Shallow copy

Now there is the Child class:

public class Child{
    //...省略其余代码
}

There is also the Person class:

public class Person implements Cloneable{
    private int age;
    private String name;
    private Child child;
    //...省略其余代码
}

Clone the Person object p1 and assign it to p2, and then let’s take a look Their toString() and hashCode() results:

public class Main {

    public static void main(String[] args) throws CloneNotSupportedException {
        Person p1 = new Person("zhangsan",23);
        p1.setChild(new Child("xiaoming", 1););
        //克隆操作
        Person p2 = p1.clone();
        //打印看看
        System.out.println(p1+", "+p1.hashCode()+", "+p1.getChild().hashCode());
        System.out.println(p2+", "+p2.hashCode()+", "+p2.getChild().hashCode());
    }
}

Output:

Person [age=23, name=zhangsan, value=666, child=Child [name=xiaoming, age=1]], 118352462, 1550089733
Person [age=23, name=zhangsan, value=666, child=Child [name=xiaoming, age=2]], 865113938, 1550089733

Conclusion: The values ​​of p1, p2 members are equal, but they are two different objects, their Child member are the same object.

Deep copy

Also implement the Cloneable interface for Child and override clone():

public class Child implements Cloneable{
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();;
    }
    //...省略其余代码
}

Then, change the Person class The implementation of clone():

public class Person implements Cloneable{
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person p = (Person)super.clone();
        p.setChild((Child)child.clone());
        return p;
    }
     //...省略其余代码
}

Then call the above main(String [] args), the result is:

Person [age=23, name=zhangsan, value=666, child=Child [name=xiaoming, age=1]], 118352462, 1550089733
Person [age=23, name=zhangsan, value=666, child=Child [name=xiaoming, age=2]], 865113938, 1442407170

Conclusion : p1 and p2 are two different objects, and their Child members are also two different objects.

Summary

Clone is to make a copy.

Shallow copy is simply copying the value. If there are reference members, their reference members will be the same object, but this is not the case with deep copy.

Why

You can think of it this way, whoever calls clone() is a deep copy of that person.

In the shallow copy example above, because clone() of the p1 object is called, p1 and p2 are two different objects (deep copies of p1 and p2 themselves); in the deep copy example , not only calls clone() of p1, but also calls clone() of Child type object, so the two Child type objects are different (deep copy of all objects).


The above is the detailed content of A brief analysis of Java cloning (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete