我想修改帐户对象,但这些更改似乎没有出现在原始列表中。也许有人可以指出一个错误。修改克隆的对象
见下面的代码:
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