In-depth java clone technology

xiaoxiao2021-03-06  71

Clone Basic Knowledge Reserve refers to Clone technology in Java, and cannot mention the java.lang.cloneable interface and an Object class containing the Clone method. All classes with Clone features have a characteristic that it implements a Cloneable interface directly or indirectly. Otherwise, we will trigger the ClonyNotSupportedException exception when trying to call the Clone () method. Below we discover and understand this feature by analyzing partial source code of the Object class. See the JDK source Object # clone () method: / * ............ * @return a clone of this instance * @exception CloneNotSupportedException if the object's class does not * support the Cloneable interface Subclasses * that override.??. the clone method can also * throw this exception to indicate that an instance can not * be cloned * @see java.lang.Cloneable * / protected native Object clone () throws CloneNotSupportedException;. description @exception part of this source confirmed The correctness of the Characteristics of Clone objects. It expresses that the object class must support the Cloneable interface, otherwise even if the derived class covers the Object # clone () method, it will also throw this exception. CLONE Realization 1. Realize the CLONEABLE interface through the previous introduction, we know that if a class is to have a Clone function, the Cloneable interface must be implemented. To do this, Clone features have been basically achieved. Clone features For us, the most important thing is to be able to use it. So how can we use the clone function? The answer is to override the Object # clone () method. 2. Override Object # clone () Method Why do you need to override the object # clone () method? It has to stand again from the JDK source code again. The prototype of the Object # clone () method is: protected native object clone () throws clonenotsupportedException; not noted that here the clone () method modifier is protected instead of public. The invisibility of this access makes us not visible to the Object # clone () method. I believe that readers have understood why the object # clone () method is to overwrite. Moreover, the modifier of the covered method must be a public, and if it is still reserved as protected, the overlay will become no practical.

The following is a simple example of a Clone function: / * * Example of classes with clone features * / public class cloneableObjexample Implements cloneable {// ... some code has omitted ... private string name = null; private int score = 0 ; / ** * NOTE: Change protected modifier to public * @see java.lang.object # clone () * / public / * protected * / Object Clone () throws clonenotsupportedException {// call parent class Clone method Object Result = super.clone (); // Todo: Custom Clone Data Return Result;}} 3. Custom Clone to this, Clone has truthfully. Clone objects we can customize it. Also on the example above. The following methods do some enhancement function: public / * protected * / Object clone () throws CloneNotSupportedException {// clone method call parent class CloneableObjExample result = (CloneableObjExample) super.clone (); // TODO: Custom clone Data // Although "clone", you can also do some adjustment result .Name = "new name"; result.score = 90; Return Result;} Clone's two types of Clone usually there are two types of shallow Clone and deep CLONE. First, analyze the differences in two. Shallow Clone and Deep Clone are Clone, which essentially defines whether the subject's property (non-primary type attribute, such as INT, etc.) is derived at Clone. If it is still reserved as a reference, it is called shallow Clone, which is called a deep clone. In fact, these two concepts are also relative concepts. They have a bit different from them, and the shallow clone mode is a Clone object. After getting the Clone object, "Clone" is required to "Clone" processing on the referenced member properties. From this level, the deep clone does not have a special difficulty. Simple talk is to create a good object and set some member properties.

About deep clone, there are too many articles on the Internet, and it is a bit of a bitter feeling. This article is no longer described. This is not the focus of this article. The focus of this article is to explain the depth clone, that is, "N Deep Clone". What exactly? The goal described herein is to have been deep to the extent you want, including deeper deeper. Implementation is combined with Java Reflection technology and recursive. The approximate step is as follows: First, Java Reflection technology is used to dynamically obtain a list of members. Then via the depth of Clone, the members with Clone conditions and the necessary Clone are also clone. This is a recursive process until the Clolne depth has until the object does not require Clone's member properties. What is a member of Clone condition and a member of the necessary Clone? For example, the primitive type, the type of transient, is not accessible (! Field # isaccessible ()), does not implement the type of the Cloneable interface, etc., there is no Clone condition. The type of Java definition such as String does not need to go deep into Clone, which belongs to unnecessary clone. But the "container" class such as the List type is a member type of the clone. According to this, the recursive program is illustrated as follows: / ** * @Return Object Return to the object * @Param obj original object * @Param Length Clone depth * / public object DeepClone (Object Object Deepclone " {Object Result = Obj; // This is pseudo code: If the object OBJ does not have a Clone condition, return Result, which is also the recursive end condition. / / Here is pseudo code: If the object OBJ is not necessary, returns Result // This is pseudo code: Start "Clone" object. This place is to adjust an abstract method to add a lot of flexibility. This method is the main purpose to implement the "clone" object scheme. Note: The "Clone" program in this may be what we want to think of, it may have a lot of ideas, but the effect is the same, it is to "clone" new object. Of course, it is the most easy thinking that the Object # clone () method is. The schematic is as follows: result = om.clone (obj); // is pseudo code: Get all members of the Clone condition and the necessary clone. This place is also an abstract method to handle it. It is also to enhance flexibility.

There are many ways to get these members, which may be obtained by setter and getter pairs, or by get Fields, etc. (this method may be many members can be directly accessible, often need to be combined with other methods), Even a combination of a variety of methods. In short, the purpose is only one, that is, these members are obtained.

For (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-92296.html

New Post(0)