Java collections — polymorphic access to elements

后端 未结 3 1974
余生分开走
余生分开走 2021-01-28 08:03

I have a LinkedHashSet of values of ThisType. ThisType is implementing the interface ThatType.

I ne

3条回答
  •  滥情空心
    2021-01-28 08:49

    I think what you want is to use wildcards.

     LinkedHashSet someFunction(LinkedHashSet set) {
          return set;
     }
    

    As has been explained elsewhere LinkedHashSet is not a subclass of LinkedHashSet, since LinkedHashSet can't accept ThatType objects.

    The wildcard in LinkedHashSet means a LinkedHastSet of some class (not sure what) that extends ThatType.

    Though you may want to consider using this:

     Set someFunction(Set set) {
          return set;
     }
    

提交回复
热议问题