How to use multiple upper bounds in generics

只愿长相守 提交于 2020-01-21 07:31:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!