java中基于超类和子类的泛型类型的多重限制

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;

    ...
}

所以现在我想使该列表的子类保持通用,但将项目限制为实现SearchListItem接口的对象:

So now I want to make 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();
}

到目前为止,我已经为SearchList类提供了以下功能:

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>

那么我需要在类声明中说什么SearchList扩展了List类并且已经对包含SomeInterface(在基类)和SearchListItem中的泛型类型的附加限制也是?

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>