Java 泛型接口应接受子类作为类型参数

Java Interface with generics shall accept subclass as typeargument

我现在有以下 class 层次结构:

interface Interface<T> {
    boolean isGreaterThan(T other);
}
class Base implements Interface<Base> {
    public boolean isGreaterThan(Base other) {
        return true;
    }
}
class Subclass extends Base {
    ... //note that I dont need to implement or overwrite isGreaterThan() here
}
class Wrapper<E extends Interface<E>> {
    protected List<E> list;
    ...
}
class Test {
    public static void main(String[] args) {
        Wrapper<Subclass> = new Wrapper<Subclass>(); //This line produces the error
    }
}

我收到以下错误消息:

Type parameter 'Subclass' is not within its bound; should implement 'Interface<Subclass>'

我的问题是:我如何告诉 java,接口应该接受任何扩展 T 的元素 E?或者是 Wrapper 的原因?我试过包装纸:

class Wrapper<E extends Interface<? extends E>> {}

wrapper 主体产生了错误,并没有改变原来的错误。

Wrapper<Base> wrapper = new Wrapper<Base>();

工作正常... 我怎样才能

Wrapper<Subclass> wrapper = new Wrapper<Subclass>();

工作也好? 有没有任何演员的干净方式? (允许使用通配符)

谢谢!

做这样的事情:

class Base<T extends Base<T>> implements Interface<T> {
    public boolean isGreaterThan(T other) {
        return true;
    }
}

并且:

class Subclass extends Base<Subclass> {
    ... //note that I dont need to implement or overwrite isGreaterThan() here
}

尝试

class Wrapper<E extends Interface<? super E>>

就像Comparable一样,直观上是一个'contra-variant' type, therefore in most cases it should be used with <? super>. For example Collections.sort