All pastes #930823 Raw Edit

NB4L1

public java v1 · immutable
·published 2008-03-06 21:02 UTC
rendered paste body
	/**	 * This method may be called to create a new copy of the	 * Object. The typical behavior is as follows:<br>	 * <ul>	 *  <li><code>o == o.clone()</code> is false</li>	 *  <li><code>o.getClass() == o.clone().getClass()</code>	 *	  is true</li>	 *  <li><code>o.equals(o)</code> is true</li>	 * </ul>	 *	 * <p>However, these are not strict requirements, and may	 * be violated if necessary. Of the three requirements, the	 * last is the most commonly violated, particularly if the	 * subclass does not override {@link #equals(Object)}.	 *	 * <p>If the Object you call clone() on does not implement	 * {@link Cloneable} (which is a placeholder interface), then	 * a CloneNotSupportedException is thrown. Notice that	 * Object does not implement Cloneable; this method exists	 * as a convenience for subclasses that do.	 *	 * <p>Object's implementation of clone allocates space for the	 * new Object using the correct class, without calling any	 * constructors, and then fills in all of the new field values	 * with the old field values. Thus, it is a shallow copy.	 * However, subclasses are permitted to make a deep copy.	 *	 * <p>All array types implement Cloneable, and override	 * this method as follows (it should never fail):<br>	 * <pre>	 *	public Object clone()	 *	{	 *		try	 *		{	 *			super.clone();	 *		}	 *		catch (CloneNotSupportedException e)	 *		{	 *			throw new InternalError(e.getMessage());	 *		}	 *	}	 * </pre>	 *	 * @return a copy of the Object	 * @throws CloneNotSupportedException If this Object does not	 *		 implement Cloneable	 * @throws OutOfMemoryError Since cloning involves memory allocation,	 *		 even though it may bypass constructors, you might run	 *		 out of memory	 * @see Cloneable	 */	//protected Object clone() throws CloneNotSupportedException	//{	//	if (this instanceof Cloneable)	//		return VMObject.clone((Cloneable) this);	//	throw new CloneNotSupportedException("Object not cloneable");	//}/*Tehat a klonozando osztalynak kotelezo jelleggel implementalnia kell a Cloneable interfeszt, kulonben hibat dob es nem kapunk klont.Ha ez megvan, akkor a super.clone() visszaadja az adott objektum klonjat Object-kent, ezt kasztolva megkapjuk a kivant tipusu klont :)Ebben az esetben a CloneNotSupportedException lekezelese igaz kotelezo,  de sosem fog elofordulni :P*/class Cloning{	public static void main(String[] args)	{		Object obj1 = new Object();		// itt azert hasal el forditaskor, mert a clone() metodus protected,		// es jelenleg nem erjuk el		//1.// Object obj2 = obj1.clone();				Cloning c1 = new Cloning();		// itt mar mukodik, hisz elerjuk		// de a default clone() metodus implementacioja miatt Object visszateresi erteke van		//2.// Cloning c2 = c1.clone();		// castolni kell, kivetelkezelni es voila :)		try		{			Cloning c3 = (Cloning)c1.clone();		}		catch (CloneNotSupportedException cnse)		{			// de ez meg mindig hibat dob, mivel nincs implementalva a Cloning interface :)			cnse.printStackTrace();		}				// HELYES		Cloning2 c21 = new Cloning2();		Cloning2 c22 = c21.clone();	}}class Cloning2 implements Cloneable{	@Override	protected/* or public*/ Cloning2 clone()	{		try		{			Object clone1 = super.clone();			Cloning2 clone2 = (Cloning2)clone1;			return clone2;		}		catch (CloneNotSupportedException cnse)		{			System.out.println("Never happens!");			return null;		}	}}