我想修改帳戶對象,但這些更改似乎沒有出現在原始列表中。也許有人可以指出一個錯誤。修改克隆的對象
見下面的代碼:
if(aBank.getAccount(number)!=null){
System.out.println("Account information is listed below");
System.out.println(aBank.getAccount(number).toString());
System.out.println("Modify first name y or n");
answer=keyboard.nextLine();
if(answer.equals("Y")||answer.equals("y")){
System.out.println("Enter first name:");
firstName=keyboard.nextLine();
aBank.getAccount(number).getCustomer().setFirstName(firstName);
}
System.out.println("Modify last name y or n");
answer=keyboard.nextLine();
if(answer.equals("Y")|| answer.equals("y")){
System.out.println("Enter last name:");
lastName=keyboard.nextLine();
aBank.getAccount(number).getCustomer().setLastName(lastName);
}
}
else{
System.out.println("Account not found");
}
注:getAccount(數字)返回這是一個深拷貝和GETCUSTOMER也返回一個克隆是深拷貝的
目錄帳戶的克隆getAccount
public Account getAccount(long accountNumber) throws Exception {
boolean found=false;
for(int i=0;i<accounts.size();i++){
if(accounts.get(i).getAccountNumber().compareTo(accountNumber)==0){
found=true;
return accounts.get(i).clone();
}
}
if (!found){
return null;
}
return null;
}
謹慎地正確縮進代碼? – aioobe 2011-06-13 14:23:39
如果您發佈了'getAccount'和'getCustomer'的內容,它會更有幫助。我懷疑你是在做一個「深層複製」。 – mre 2011-06-13 14:24:47
public Account getAccount(long accountNumber)throws Exception { \t \t boolean found = false; \t \t對(INT I = 0; I
trs
2011-06-13 14:29:58