2013-10-05 59 views
0

我希望能够将2个泛型传递给我的类。Java和泛型的使用?

  • 首先泛型类型可以是任何
  • 二泛型类型必须是某个对象的列表。

我该如何做到这一点?下面的代码不能编译,它只是显示了我的目标。

public class AbstractGroupedAdapter<T, List<Y>> extends ArrayAdapter<Y> { 

    protected Map<T, List<Y>> groupedItems; 

    protected T getHeaderAtPosition(int position) { 
     // return the correct map key 
    } 

    protected Y getItemAtPosition(int position) { 
     // return the correct map value 
    } 

    @Override 
    public int getCount() { 
     return groupedItems.size() + groupedItems.values().size(); 
    } 
} 
+0

尝试'public class AbstractGroupedAdapter extends etc' –

回答

2

在Java中,您不能在它们的名称声明中限定泛型类型参数。相反,正常声明类型参数并将其用于通用边界,即:

public class AbstractGroupedAdapter<T,Y> extends ArrayAdapter<List<Y>> { 


    protected Map<T, List<Y>> groupedItems; 

    protected T getHeaderAtPosition(int position) { 
     // return the correct map key 
    } 

    protected Y getItemAtPosition(int position) { 
     // return the correct map value 
    } 

    @Override 
    public int getCount() { 
     return groupedItems.size() + groupedItems.values().size(); 
    } 
}