Java-clone使用场景
在写bean类的时候,变量有可能是一个引用数据类型,比如Date
public class Person {
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
-
然后对这个类进行测试
public class CloneTest { public static void testClone(){ Person person = new Person(); person.setDate(new Date()); System.out.println("before"+person.getDate()); Date getDate = person.getDate(); getDate.setTime(1111111111); System.out.println("after"+person.getDate()); System.out.println("getDate = " + getDate); } public static void main(String[] args) { testClone(); } } -
运行输出
beforeSun May 05 11:54:31 CST 2019 afterWed Jan 14 04:38:31 CST 1970 getDate = Wed Jan 14 04:38:31 CST 1970 -
可以看到,getDate改变数据,直接导致person中的date变量也改变了
-
因为date和getDate引用的是同一个对象

-
使用clone方法

- 测试运行输出
beforeSun May 05 12:20:57 CST 2019
afterSun May 05 12:20:57 CST 2019
getDate = Wed Jan 14 04:38:31 CST 1970
- 可见getDate和person.date并没有影响了
博客介绍了Java clone方法的使用场景。在编写bean类时,变量可能为引用数据类型,如Date。测试发现,若不使用clone方法,修改引用对象会影响原对象;使用clone方法后,两者互不影响。
1038

被折叠的 条评论
为什么被折叠?



