这是克隆数组的正确方法吗?我想创建一个AccountWithInterface的深层副本,其中包含一个定义的克隆方法。我已经阅读了深拷贝与浅拷贝之间的区别所以请不要解释它们之间的差异。它的正确语法,我现在遇到了麻烦。
// create object
AccountWithInterface accountTemplate = new AccountWithInterface(name, balance, id, rate);
// declare object
AccountWithInterface[] accountArray = new AccountWithInterface[3];
// create each accountArray object
for(int i = 0; i < accountArray.length; i++) {
accountArray[i] = new AccountWithInterface();
// copy each element (object)
accountArray[i] = (AccountWithInterface)accountArray[i].clone();
}
public Object clone() throws CloneNotSupportedException {
AccountWithInterface acctClone = (AccountWithInterface)super.clone();
return acctClone;
}