Java-related cloning and "Refrigerating" and "unfreezing" methods [00 original]

Source: Internet
Author: User
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;
}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.