问题
I have a Interface Foo which has a generic type -
public interface Foo<T> {
boolean apply(T t);
}
Having another class Bar which implements this interface but what I want is the generic Type of Bar should be a Collection of type Interface A and B, With the below definition its giving compiler error -
public class Bar implements Foo<Collection<? extends A & B>>{
@Override
public boolean apply(Collection<? extends A & B> collect){
...
}
}
Can you suggest the correct way to achieve this?
I can use multiple bounds at method level only?
回答1:
Wouldn't this work?
public class Bar<T extends A & B> implements Foo<Collection<T>>{
@Override
public boolean apply(Collection<T> collect){
...
}
}
来源:https://stackoverflow.com/questions/5034231/how-to-use-multiple-upper-bounds-in-generics