Package prototype;
/*
* Prototype mode definition: Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.
* The Prototype mode allows an object to create another custom object without any details on how to create it.
* Working principle: Send a prototype object to the object to be created. The object to be created is requested
* Type objects are copied and created by themselves.
*/
Public abstract class Monkey implements Cloneable {
Private String monName;
Public void setMonName (String monName ){
This. monName = monName;
}
Public String getMonName (){
Return monName;
}
@ Override
Protected Object clone () throws CloneNotSupportedException {
Object obj = null;
If (obj = null)
Obj = super. clone ();
Return obj;
}
} Www.2cto.com
Package prototype;
Public class Human extends Monkey {
Public Human ()
{
SetMonName ("human ");
}
Public static void main (String [] args) throws Exception
{
Monkey mk = new Human ();
System. out. println (mk. getMonName ());
Monkey mk2 = (Monkey) mk. clone ();
System. out. println (mk2.getMonName ());
System. out. println ("equal:" + mk. equals (mk2 ));
}
}