gpt4 book ai didi

java - 绑定(bind)不匹配 : The type Foo is not a valid substitute

转载 作者:行者123 更新时间:2023-11-29 05:24:38 24 4
gpt4 key购买 nike

我需要做类似的事情:

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> {

}

但是,我在“class A extends 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>

这怎么可能?当我尝试计算泛型时,它看起来是正确的。

最佳答案

让我们把它画出来。在

class A extends AbstractA<Foo> {

类型参数 Foo 应该匹配

class AbstractA<PARAM extends AbstractB<AbstractA<PARAM>>> {
^
|
Foo

Foo 在哪里

class Foo extends AbstractB<A> {

到目前为止,Foo 扩展了 AbstractB。让我们看看它的类型参数。

class AbstractB<T extends AbstractA<?>> {
^
|
A

那么什么是A?它是否匹配那些界限

class A extends AbstractA<Foo> {

似乎因为 Foo 可以适合通配符。

但是,当Foo用作类型参数时,需要考虑通配符,所以它有点像下面的继承(和泛型)层次结构

Foo extends AbstractB<A extends AbstractA<?>>

这不符合

的大图
PARAM extends AbstractB<AbstractA<PARAM>>

所以 Foo 不能用作那里的类型参数。

关于java - 绑定(bind)不匹配 : The type Foo is not a valid substitute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23159634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com