子类只能调用受保护的clone方法克隆自己。因此,必须重新定义clone方法,并将它声明为public,这样才能让所有的方法克隆对象。
public class Employee implements Cloneable{ @Override PRotected Employee clone() throws CloneNotSupportedException { /* //浅拷贝 return (Employee) super.clone(); */ //深拷贝 Employee cloned = (Employee) super.clone(); //拷贝对象 cloned.hireDay = (Date) hireDay.clone();//拷贝可变的域 return cloned; }}新闻热点
疑难解答