¿¹Á¦

public static class Base implements Cloneable {
public void setValue( int value ) {
this .value = value;
}
public int getValue() {
return value;
}
public Object clone() throws CloneNotSupportedException {
return super .clone();
}
private int value = 0;
}

public static class Derived extends Base implements Cloneable {

public void setString( String str ) {
this .str = str;
}
public String getString() {
return str;
}
public Object clone() throws CloneNotSupportedException {
Derived clone = new Derived();
clone.str = str;
return clone;
}
private String str;
}

public static void main(String[] args){
Derived original = new Derived();
original.setValue( 10 );
original.setString( "ABC" ); //$NON-NLS-1$
System.out.println( "Original: " + //$NON-NLS-1$
original.getString() + "," + original.getValue() ); //$NON-NLS-1$
try {
Derived clone = (Derived)original.clone();
System.out.println( "Clone: " + //$NON-NLS-1$
clone.getString() + "," + clone.getValue() ); //$NON-NLS-1$
}catch ( CloneNotSupportedException e ) {
e.printStackTrace();
}
}


¼Ö·ç¼Ç
clone()¿¡¼­ super.clone()À» È£ÃâÇϽʽÿÀ.


public static class Base implements Cloneable {

public void setValue( int value ) {
this .value = value;
}
public int getValue() {
return value;
}
public Object clone() throws CloneNotSupportedException {
return super .clone();
}
private int value;

}

public static class Derived extends Base implements Cloneable {
public void setString( String str ) {
this .str = str;
}
public String getString() {
return str;
}
public Object clone() throws CloneNotSupportedException {
Derived clone = (Derived)super .clone();
clone.str = str;
return clone;
}
private String str;
}

public static void main(String[] args){
Derived original = new Derived();
original.setValue( 10 );
original.setString( "ABC" ); //$NON-NLS-1$
System.out.println( "Original: " + //$NON-NLS-1$
original.getString() + "," + original.getValue() ); //$NON-NLS-1$
try {
Derived clone = (Derived)original.clone();
System.out.println( "Clone: " + //$NON-NLS-1$
clone.getString() + "," + clone.getValue() ); //$NON-NLS-1$
}catch ( CloneNotSupportedException e ) {
e.printStackTrace();
}
}