I need to do something like:
我需要做一些事情,比如:
class AbstractA<PARAM extends AbstractB<AbstractA<PARAM>>> {
public AbstractA(PARAM arg) {
arg.doStuff(this);
}
}
class AbstractB<T extends AbstractA<?>> {
public void doStuff(T onWhat) {
System.out.println(onWhat);
}
}
class A extends AbstractA<Foo> {
public A(Foo arg) {
super(arg);
}
}
class Foo extends AbstractB<A> {
}
However, I'm getting this error at "class A extends AbstractA {":
但是,我在“类A扩展AbstractA{”中得到这个错误:
Bound mismatch: The type Foo is not a valid substitute for the bounded
parameter <PARAM extends AbstractB<AbstractA<PARAM>>> of the
type AbstractA<PARAM>
How's that possible? When i try to work out the generics, it looks correct.
那怎么可能?当我试着算出泛型时,它看起来是正确的。
1 个解决方案
#1
3
Let's draw it out. In
让我们画出来。在
class A extends AbstractA<Foo> {
the type argument Foo
is supposed to match
类型参数Foo应该匹配
class AbstractA<PARAM extends AbstractB<AbstractA<PARAM>>> {
^
|
Foo
where Foo
is
Foo在哪里
class Foo extends AbstractB<A> {
Up to now, Foo
extends AbstractB
. Let's see for its type arguments.
到目前为止,Foo扩展了AbstractB。让我们看看它的类型参数。
class AbstractB<T extends AbstractA<?>> {
^
|
A
So what is A
? Does it match those bounds
那么是什么呢?它符合这些界限吗?
class A extends AbstractA<Foo> {
It seems to since Foo
can fit the wildcard.
似乎因为Foo可以匹配通配符。
However, when Foo
is used as a type argument, the wildcard needs to be taken into account, so it kind of looks like the following inheritance (and generics) hierarchy
但是,当Foo用作类型参数时,需要考虑通配符,因此它看起来像下面的继承(和泛型)层次结构。
Foo extends AbstractB<A extends AbstractA<?>>
which does not fit the bigger picture of
哪个不符合大局
PARAM extends AbstractB<AbstractA<PARAM>>
So Foo
cannot be used as a type argument there.
所以Foo不能用作类型参数。
#1
3
Let's draw it out. In
让我们画出来。在
class A extends AbstractA<Foo> {
the type argument Foo
is supposed to match
类型参数Foo应该匹配
class AbstractA<PARAM extends AbstractB<AbstractA<PARAM>>> {
^
|
Foo
where Foo
is
Foo在哪里
class Foo extends AbstractB<A> {
Up to now, Foo
extends AbstractB
. Let's see for its type arguments.
到目前为止,Foo扩展了AbstractB。让我们看看它的类型参数。
class AbstractB<T extends AbstractA<?>> {
^
|
A
So what is A
? Does it match those bounds
那么是什么呢?它符合这些界限吗?
class A extends AbstractA<Foo> {
It seems to since Foo
can fit the wildcard.
似乎因为Foo可以匹配通配符。
However, when Foo
is used as a type argument, the wildcard needs to be taken into account, so it kind of looks like the following inheritance (and generics) hierarchy
但是,当Foo用作类型参数时,需要考虑通配符,因此它看起来像下面的继承(和泛型)层次结构。
Foo extends AbstractB<A extends AbstractA<?>>
which does not fit the bigger picture of
哪个不符合大局
PARAM extends AbstractB<AbstractA<PARAM>>
So Foo
cannot be used as a type argument there.
所以Foo不能用作类型参数。