先附上两端小代码
public class Course {
void put()
{
System.out.println("here");
}
public static void main(String[] args) throws CloneNotSupportedException {
Course a=new Course();
Course b=(Course)a.clone();
a.put();
b.put();
}
}
public class Course implements Cloneable {
void put()
{
System.out.println("here");
}
public static void main(String[] args) throws CloneNotSupportedException {
Course a=new Course();
Course b=(Course)a.clone();
a.put();
b.put();
}
}
其中第一段代码运行会运行错误,而第二段能够正常运行;
这里我感到很迷惑,第一段中调用的clone()应该是Object中已经帮你实现好的clone()方法,而第二段中调用的clone()方法是应该是Clonealbe接口中的方法,那里的clone()应该是未被定义的,按理来说应当报错;
最后结果反而第二段能够正常运行而第一段却报错,十分不解;
希望有人能够为我指点迷津!