开发者

Multiple restrictions on generic type based on super and sub classes in java

I have a generic list class that implements a particular interface.

The list items in the interface also implement the same interface.

public abstract class List<T extends SomeInterface> implements SomeInterface
{
    protected LinkedList<T> m_list;

    ...
}

So now I want to ma开发者_运维问答ke a subclass of this list that stays generic but limits the items to objects that implement the SearchListItem interface:

public interface SearchListItem
{
    public String getName();
}

Here's what I have for the SearchList class so far:

public abstract class SearchList<T extends SearchListItem> extends List<T>
{
    public T get(String name)
    {
        ...
    }

    ...
}

But of course this complains on the definition of the class:

Bound mismatch: The type T is not a valid substitute for the bounded parameter <T extends SomeInterface> of the type List<T>

So what do I need to put into the class declaration to say "SearchList extends the List class and has additional restrictions on the generic class type that includes both SomeInterface (in the base class) and SearchListItem as well"?

Please tell me if I can reword this to help explain it.


Does this work?

public abstract class SearchList<T extends SomeInterface & SearchListItem> extends List<T>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜