我不知道为什么以下程序会产生此错误 - > clone has protected access in Object
。派生类不能访问Object类的受保护方法吗?如果我跳过此语句cloned.id=(String)id.clone();
,程序将成功编译!
class Example implements Cloneable{
String id;
void display(){
System.out.println("ID="+id);
}
public Example clone() throws CloneNotSupportedException{
Example cloned;
cloned=(Example)super.clone();
cloned.id=(String)id.clone(); //troublemaker
return cloned;
}
}
class CloneInterface{
public static void main(String args[]){
try
{
Example e1=new Example();
e1.id="a";
Example e2=e1.clone();
e2.id="b";
e1.display();
e2.display();
}
catch(CloneNotSupportedException e)
{
e.printStackTrace();
}
}
}