我正在为以下问题寻找一个优雅的解决方案:
//one interface
public interface MyInterface () {
}
//two implementations
public class ImplA implements MyInterface (){
}
public class ImplB implements MyInterface () {
}
在另一个类中:
//one generic method
public void myMethod(Class<MyInterface>... myTypes) {
for (Class<MyInterface> myType : myTypes) {
System.err.println("my Type:" +myType);
}
}
问题是您不能简单地调用此方法:
myMethod(ImplA.class, ImplB.class);
这根本不被接受。可选参数和泛型真的不能结合吗?我找不到任何例子。
最佳答案
我会努力
public void myMethod(Class<? extends MyInterface>... myTypes) {
Class<MyInterface>
必须是 MyInterface.class
不是子类。
关于java - Java中的泛型+可选参数不能组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12283119/