比如一个对象AccountEntry entry。想新建一个和它内容一样的对象,但是要互不影响。
AccountEntry entryPre=entry 这种写法是不能满足要求的,因为它们的内存地址是一样的,其中一个对象有改动,另一个对象也会跟着改。
如果要互不影响,应该:
在AccountEntry 中实现implements Cloneable接口,并且重写clone方法。然后 entryPre=entry.clone();
public AccountEntry clone() {
AccountEntry newEntry = new AccountEntry();
newEntry.customerId = this.customerId;
........
return newEntry;
}