All classes in the Java language inherit from the Java.lang.Object class by default, and in the Java.lang.Object class there is a clone () method, and the description of the JDK API explains that this method returns a copy of the object. We need to explain two points: first, the Copy object returns a new object, not the reference address of an object, and the difference between a copy object and a new object returned with the keyword operator is that the copy already contains information about the original object, not the object's initial information. That is, each copy action is not created for a new object.
When we use the New keyword to create an instance of a class, all constructors in the constructor are automatically called. But if an object implements the Cloneable interface, then we can call its clone () method, noting that the Clone () method does not call any constructors.
Code 3-1 shows a typical implementation of the factory pattern, where the factory pattern is a pattern that replaces the new operation with a factory method, so the factory pattern acts as the new operator that creates the instance object.
Code Listing 3-1 Creating a new object
public static Creditgetnewcredit ()
{
return new credit ();//Create a new credit object
}
If we use the Clone () method to create the object, the original information can be preserved, so the creation speed will be accelerated. As shown in Listing 3-2, the improved code uses the Clone () method.
Code Listing 3-2 uses the Clone () method
private static Creditbasecredit = new Credit ();
public static credit Getnewcredit ()
{
return (Credit) Basecredit.clone ();
}
Please pay attention to Uncle Mike every 10 o'clock in the evening said, let us communicate and study together.