Java collections — polymorphic access to elements

后端 未结 3 2000
余生分开走
余生分开走 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:31

    You need the wildcard of Set. Look at following example

    public static void main(String[] args) {
        Set set=new LinkedHashSet<>();
        Set set2=new LinkedHashSet<>();
    
        someFunction(set);
        someFunction(set2);
    }
    
    static void someFunction(Set set) {
    
    }
    
    class ThisType implements ThatType{
    
    }
    interface ThatType{
    
    }
    

提交回复
热议问题