Overloading Java function with List<> parameter

前端 未结 6 1283
猫巷女王i
猫巷女王i 2021-02-02 15:19

I have 2 classes

public class Customer{
  ...
  public String getCustomerNumber();
  ...
}

public class Applicant{
   ....
   private Customer c;
   public Cust         


        
6条回答
  •  温柔的废话
    2021-02-02 15:52

    One way to workaround this issue would be to define custom list types like this:

    class CustomerList extends ArrayList {
        ...
    }
    
    class ApplicantList extends ArrayList {
        ...
    }
    

    Then the following overloading would be legal:

    public void processCustomerNumbers(CustomerList custList)
    
    public void processCustomerNumbers(ApplicantList appList)
    

    However, I don't think that this would be a good idea. For a start, it hardwires particular implementation classes into your application's APIs.

    A better approach is to define a common interface for Customer and Applicant that allows you to process them with one processCustomerNumbers method. (As described at length in other answers.)

提交回复
热议问题