Java 编程:使用 List 接口优化 Customer 和 Bank 类
package step3;
import java.util.ArrayList; import java.util.List;
public class Customer extends Object {
private String firstName;
private String lastName;
'******** Begin *'
private List
public Customer(String ln, String sn) {
this.lastName = ln;
this.firstName = sn;
'******** Begin *********'
// accounts = new ArrayList<>(10);
'******** End *********'
}
// getter方法
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getNumberOfAccounts() {
'******** Begin *********'
return accounts.size();
'******** End *********'
}
public String toString() {
return "[" + lastName + ", " + firstName + "]";
}
// 业务逻辑方法:
public void addAccount(double amount) {
'******** Begin *********'
accounts.add(new Account(amount));
'******** End *********'
}
public void addAccount(Account account) {
'******** Begin *********'
accounts.add(account);
'******** End *********' }
// 查看某一个账户的信息
public Account getAccount(int index) {
'******** Begin *********'
return accounts.get(index);
'******** End *********' }
} package step3;
import java.util.ArrayList; import java.util.List;
public class Bank {
// 静态成员变量及初始化
'******** Begin *'
private static List
public static int getNumberOfCustomers() {
'******** Begin *********'
return customers.size();
'******** End *********'
}
public static void addCustomer(String ln, String fn) {
'******** Begin *********'
customers.add(new Customer(ln, fn));
'******** End *********'
}
public static Customer getCustomer(int index) {
'******** Begin *********'
return customers.get(index);
'******** End *********'
}
}
原文地址: https://www.cveoy.top/t/topic/nEQC 著作权归作者所有。请勿转载和采集!