Import java. AWT. Point;
Import java. Io. ioexception;
Import com. Sun. CORBA. se. impl. Io. optionaldataexception;
/**
* Clone test <br>
* Taking the Square type as an example, the similarities and differences between deep clone and shallow clone are compared.
*
* @ See # clone ()
* @ Author 88250
* @ Version 1.0.0, 2007-8-26
*/
Public class clonetester
{
Private square = New Square ();
Private square cpysquare = NULL;
/**
* Shortest cloning
*/
Public void shallowclone ()
{
Square. setsidelength (2 );
Square. setlocation (new point (2, 5 ));
// Shortest clone
Cpysquare = (square) Square. Clone ();
}
/**
* Deep clone operation
*/
Public void deepclone ()
{
Square. setsidelength (3 );
Square. setlocation (new point (1, 3 ));
// Deep clone
Try
{
Cpysquare = (square) Square. deepclone ();
}
Catch (optionaldataexception E)
{
E. printstacktrace ();
}
Catch (ioexception E)
{
E. printstacktrace ();
}
Catch (classnotfoundexception E)
{
E. printstacktrace ();
}
}
/**
* Clone result output
*/
Public void clonedisplay ()
{
System. Out. println ("original square length:" + square. getsidelength ());
System. Out. println ("cloned square length:" + cpysquare. getsidelength ());
System. Out. println ("original square = cloned square? "+ (Square = cpysquare ));
System. Out. println ("Location of the original square = Location of the cloned square? "
+ (Square. getlocation () = cpysquare. getlocation ()));
}
Public static void main (string [] ARGs)
{
Clonetester Sm = new clonetester ();
SM. shallowclone ();
SM. clonedisplay ();
SM. deepclone ();
SM. clonedisplay ();
}
}
Import java. AWT. Point;
Import java. Io. bytearrayinputstream;
Import java. Io. bytearrayoutputstream;
Import java. Io. ioexception;
Import java. Io. objectinputstream;
Import java. Io. objectoutputstream;
Import java. Io. serializable;
Import com. Sun. CORBA. se. impl. Io. optionaldataexception;
/**
* Square
*
* @ Author 88250
* @ Version 1.0.0, 2007-8-26
*/
Public class square implements cloneable, serializable
{
Private point location = new point (0, 0 );
Private float sidelength = 1f;
@ Override
Public object clone ()
{
Square TMP = NULL;
Try
{
TMP = (square) Super. Clone ();
}
Catch (clonenotsupportedexception cnse)
{
Cnse. printstacktrace ();
}
Finally
{
Return TMP;
}
}
/**
* Deep cloning method
* @ Return
*/
Public object deepclone ()
Throws ioexception, optionaldataexception, classnotfoundexception
{
// First write the object to the stream
Bytearrayoutputstream BO = new bytearrayoutputstream ();
Objectoutputstream oo = new objectoutputstream (BO );
OO. writeobject (this );
// Then read the object from the stream
Bytearrayinputstream Bi = new bytearrayinputstream (BO. tobytearray ());
Objectinputstream OI = new objectinputstream (BI );
Return (OI. readobject ());
}
/**
* @ Return the location
*/
Public point getlocation ()
{
Return location;
}
/**
* @ Param location the location to set
*/
Public void setlocation (point location)
{
This. Location = location;
}
/**
* @ Return the sidelength
*/
Public float getsidelength ()
{
Return sidelength;
}
/**
* @ Param sidelength the sidelength to set
*/
Public void setsidelength (float sidelength)
{
This. sidelength = sidelength;
}
}