I have 2 classes
public class Customer{
...
public String getCustomerNumber();
...
}
public class Applicant{
....
private Customer c;
public Customer getCustomer(){ return c; }
...
}
When presented with a list of customers or applicants I want a function which iterates the list and does something with the CustomerNumber.
I've tried overloading the function
public void processCustomerNumbers(List custList)
...
public void processCustomerNumbers(List appList)
...
but these are seen as duplicate methods... is there a nice way of doing this rather than just having 2 differently named functions?
解决方案
If you make both classes implement a common interface,
interface CustomerNumber {
String getCustomerNumber();
}
public class Customer implements CustomerNumber {
...
public String getCustomerNumber();
...
}
public class Applicant implements CustomerNumber {
....
private Customer c;
public Customer getCustomer() { return c; }
public String getCustomerNumber() { return getCustomer().getCustomerNumber(); }
...
}
then you might be able to do what you want with just a single method:
public void processCustomerNumbers(List extends CustomerNumber> appList) {
for (Customer c: appList) {
processCustomerNumber(c.getCustomerNumber());
}
}