使用 List<> 参数重载 Java 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2241514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:16:15  来源:igfitidea点击:

Overloading Java function with List<> parameter

javalistfunctionarguments

提问by MadMurf

I have 2 classes

我有2节课

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.

当出现客户或申请人列表时,我需要一个函数来迭代列表并使用 CustomerNumber 执行某些操作。

I've tried overloading the function

我试过重载函数

public void processCustomerNumbers(List<Customer> custList)
...

public void processCustomerNumbers(List<Applicant> appList)
...

but these are seen as duplicate methods... is there a nice way of doing this rather than just having 2 differently named functions?

但这些被视为重复的方法......有没有一种很好的方法来做到这一点,而不仅仅是拥有 2 个不同命名的函数?

回答by finnw

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());
    }
}

回答by danben

The thing about generics in Java is that generic types are erased at runtime, so both of these methods compile to the same signature. You will need to have separate method names, or check the type of the list elements at runtime.

Java 中关于泛型的事情是泛型类型在运行时被删除,因此这两种方法编译为相同的签名。您需要有单独的方法名称,或者在运行时检查列表元素的类型。

回答by Anon.

Generics have what is known as type erasure- List<Customer>and List<Applicant>are the same type, the compiler just places compile-time restrictions on what you can do with them.

泛型具有所谓的类型擦除-List<Customer>并且List<Applicant>是相同的类型,编译器只是对您可以使用它们做什么设置编译时限制。

You could check the type of the first object in the list and call a (differently-named) internal method based on that.

您可以检查列表中第一个对象的类型,并基于此调用(不同名称的)内部方法。

回答by Stephen C

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

解决此问题的一种方法是定义自定义列表类型,如下所示:

class CustomerList extends ArrayList<Customer> {
    ...
}

class ApplicantList extends ArrayList<Applicant> {
    ...
}

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.

但是,我认为这不是一个好主意。首先,它将特定的实现类硬连接到应用程序的 API 中。

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

更好的方法是为客户和申请人定义一个通用接口,允许您使用一种processCustomerNumbers方法处理它们。(如其他答案中详细描述的那样。)

回答by Jacob

Use array instead.

改用数组。

public void processCustomerNumbers(Customer[] custList)
...

public void processCustomerNumbers(Applicant[] appList)
...

When you try to call these methods with a list, convert the list to array:

当您尝试使用列表调用这些方法时,请将列表转换为数组:

List<Customer> customers;
List<Applicant> applicants;
...
processCustomerNumbers(customers.toArray(new Customer[]{});
processCustomerNumbers(applicants.toArray(new Applicant[]{});

回答by Sreejesh

Before coming into the method names , the class hierarchy is little bit confusing...

在进入方法名称之前,类层次结构有点混乱......

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

public class Applicant{          
   ....          
   private Customer c;          
   public Customer getCustomer(){ return c; }          
   ...          
}    

Why should applicant and Customer be different objects ? Can you tell the relation between these objects ?

为什么申请人和客户应该是不同的对象?你能说出这些对象之间的关系吗?