简体   繁体   English

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

[英]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 make a subclass of this list that stays generic but limits the items to objects that implement the SearchListItem interface: 所以现在我想让这个列表的子类保持通用,但是将项限制为实现SearchListItem接口的对象:

public interface SearchListItem
{
    public String getName();
}

Here's what I have for the SearchList class so far: 这是我到目前为止为SearchList类所拥有的:

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"? 那么我需要在类声明中说“SearchList扩展List类并对包含SomeInterface(在基类中)和SearchListItem的泛型类类型有其他限制”?

Please tell me if I can reword this to help explain it. 请告诉我,我是否可以改写这个以帮助解释它。

这有用吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM