11th: Carefully overwrite the Clone () method

Source: Internet
Author: User

To implement a clone, a class needs to implement the Cloneable interface, which indicates that the object of this class has the function of cloning.

The Cloneable interface is an Mixin interface that does not have any abstract methods, similar interfaces have serializable interfaces, which indicate that objects of this class can be serialized.

The concept of another object should be explicitly cloned from one object: By cloning another object from one object, referred to as Object 1 and Object 2, the two objects become two separate objects.

If you make any modifications to object 1, object 2 should not be affected, and object 1 should not be affected by any modification to object 2. This requires that the instance domain of object 1 and Object 2 be independent of each other.

Then we know the instance domain of an object, including the base type (Int,long. And so on), the domain of the reference type (divided into mutable fields and final immutable fields).

Now understand the Clone () method of the object class:

Protected native Object clone () throws Clonenotsupportedexception;

The Clone () method is a protected local method.

The general constraints of the Clone () method are as follows:

X.clone ()! = x should return True

X.clone (). GetClass () = = X.getclass () should return true

X.clone (). Equals (x) should return True

For a class that implements the Cloneable () interface, which is a class with cloning capability, we always expect it to provide a functionally appropriate public clone () method.

If we only call Super.clone () in the Clone () method of a class and then return, it simply returns a new object that has the same primitive type field as the original object.

However, the reference type field of this object refers to the object referenced by the reference type field of the original object. This causes a problem where modifications to the domain of the original object reference type affect

The reference type field to the new object. This means that the Clone method of the object class is called, only the creation of the new object is completed, and the clone of the primitive Type field, but the reference to the Type field is

An instance of the reference type field that points to the old object. Such as:

But there is a situation where such a clone is allowed, and the class reference type field has only a final immutable type. Because the final reference type field of Object 1 is only referenced to an instance, it is not allowed to change.

In this way, the domain of object 1 has to be changed and only the base type domain, and the basic type domain we have completed cloning.

In short, all classes that implement the Cloneable interface should have a public method that overrides the Clone () method inherited from the object class, which first calls Super.clone ().

Get a "shallow copy" object and then fix any fields that need remediation based on the instance domain of the original object. In general, this means copying any of the internal "deep structures"

Variable object. and replace references to the new objects with references to the objects that originally pointed to them. Although, these internal copy operations can often be done through a layer of progressive invocation of clone ()

The Apple and dog classes must also correctly provide the public clone () method, but this is often not the best approach. If the class contains only the base type of the domain, and the immutable type of the reference field,

In most cases, no domain needs to be fixed.

11th: Carefully overwrite the Clone () method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.