1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class Test { public static void main(String[] args) throws CloneNotSupportedException { String name = "Java 设计模式"; Date publishDate = new Date();
Book book = new Book(name, publishDate); //克隆出对象 Book clone = (Book) book.clone();
System.out.println(book); System.out.println(book.hashCode()); System.out.println(clone); System.out.println(clone.hashCode());
System.out.println("==================");
publishDate.setTime(publishDate.getTime() + 2000);//修改时间 System.out.println(book); System.out.println(book.hashCode()); System.out.println(clone); System.out.println(clone.hashCode());
/** * 测试结果:修改了时间,源对象和克隆的对象的时间,都发生了改变 * Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:33 CST 2021} * 2093631819 * Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:33 CST 2021} * 2074407503 * ================== * Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:35 CST 2021} * 2093631819 * Book{name='Java 设计模式', publishDate=Wed Dec 29 12:17:35 CST 2021} * 2074407503 */ } }
|