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" );
System.out.println( "Original : " +
original.getString() + "," + original.getValue() );
try {
Derived clone = (Derived)original.clone();
System.out.println( "Clone : " +
clone.getString() + "," + clone.getValue() );
}catch ( CloneNotSupportedException e){
e.printStackTrace();
}
}
|
|