In-depth shallow Java Clone technology (1)

zhaozj2021-02-16  115

This is the first one of Clone technology introduction. This article mainly introduces the basic knowledge of the object Clone technology.

Clone Basic Knowledge Reserve

In Java mentioned Clone technology, you can't 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. Please see the source code of the Object # clone () method in the JDK:

/ *

..........

* @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;

The description of this source of the @Exception section confirms the correctness of the discrimination of Clone object characteristics above. 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. With regard to overriding clone () methods, subsequent articles will be more detailed in detail using specialty.

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

New Post(0)