英文:
Why create a temp here?
问题
这里创建临时变量 "temp" 的目的是访问 "um.userList" 中的特定用户对象,以便在后续代码中打印该用户的帐户信息。
英文:
what is the purpose of creating a temp here?
void printAcc(int identifier) {
User2 temp = um.userList[identifier];
System.out.println("=====================");
System.out.println("ID : " + temp.id);
System.out.println("=====================");
for (int i = 0; i < temp.accCnt; i++) {
System.out.println("accNumber: " + temp.acc[i].accNumber + "/ money: " + temp.acc[i].money);
}
System.out.println("=================================\n");
}
答案1
得分: 1
该变量可以存在,以便您无需在整个方法中重复表达式 um.userList[identifier]。如果您以后需要更改此值的检索方式(例如,将 userList 更改为 List 而不是数组),这将非常有帮助。
如果您给它一个比 temp 更好的名称,例如 user 或 accountHolder,它甚至可以作为变量的文档,说明变量的作用。
英文:
The variable can be there so you don't have to repeat the expression um.userList[identifier] all over the method. This is helpful if you ever need to change the way this value is retrieved (e.g. userList is changed to be a List instead of an array)
If you give it a better name than temp, e.g. user or accountHolder, it even serves as documentation what the variable does.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论