class cloneClass implements Cloneable{
int x=0;
int y=9;
protected void protectedMethod()
{
System.out.println("calling ProtectedMethod");
}
Object getObj() throws CloneNotSupportedException
{
return clone();
}
}
class extendProtectedClass extends cloneClass{
}
public class Test{
public static void main(String [] args){
cloneClass cl=new cloneClass();
try{
cloneClass cl2=(cloneClass)cl.clone();//通过cl.clone()条用Object.clone()方法出错
System.out.println(cl2.x);
}catch(CloneNotSupportedException ex){
ex.printStackTrace();
}
/*extendProtectedClass ep=new extendProtectedClass();
ep.protectedMethod(); //可以通过子类的对象直接调用父类的保护方法protectedMethod()
*/
}
} |
|