您必须为这两个私有数组创建get方法
public Customer[] getCustomers() {
return customers;
}
public Employee[] getEmployees() {
return employees;
}
这样,您可以从其他班级访问它们。或者,如果您想从特定索引中获取值。
public Customer getCustomer(int index) {
return customers[index];
}
public Employee getEmployee(int index) {
return employees[index];
}
但是在这种情况下,您还必须处理IndexOutOfBoundException情况。为避免此错误,一种方法是检查索引是否小于数组的长度且大于零,否则将返回null值。
public Customer getCustomer(int index) {
if(index >= 0 && index < customers.lenght) {
return customers[index];
}
return null;
}
public Employee getEmployee(int index) {
if(index >= 0 && index < employees.lenght) {
return employees[index];
}
return null;
}