在java书籍和在线教程中,Object.clone()除非使用Cloneable接口,否则方法提供浅拷贝,但在我实现的代码中clone()不使用Cloneable接口的方法,它提供深拷贝而不是浅拷贝。
import java.util.GregorianCalendar;
public class test1 {
public static void main(String[] args) {
// create a gregorian calendar, which is an object
GregorianCalendar cal = new GregorianCalendar();
// clone object cal into object y
GregorianCalendar y = (GregorianCalendar) cal.clone();
// check if reference of y is equal to cal or not
System.out.println(cal==y);//it's output should be true if this is a shallow copy but it is false.
}
}
HUX布斯
相关分类