【发布时间】:2017-05-22 08:04:58
【问题描述】:
我有以下示例无法编译并生成Error:() java: type argument GroupOfPartsDecorImpl<V> is not within bounds of type-variable GOP。代码如下:
class MainContainerGroupPartDecorator<V, GOP extends GroupOfParts<V, PartDecorator<V, ? extends Part<V>>>>
extends BaseContainerGroupPartDecorator<V, GOP> {
public static <V> MainContainerGroupPartDecorator<V, GroupOfPartsDecorImpl<V>> getInstance() {
return null;
}
}
class BaseContainerGroupPartDecorator<V, GOP extends GroupOfParts<V, ?>> {
void something() {}
}
class GroupOfPartsDecorImpl<V> implements GroupOfParts<V, PartDecorator<V, PartImpl1<V>>> {
@Override
public Collection<PartDecorator<V, PartImpl1<V>>> getParts() {
return null;
}
}
interface GroupOfParts<V, P extends Part<V>> {
Collection<P> getParts();
}
class PartDecorator<V, P extends Part<V>> implements Part<V> {
@Override
public V getId() {
return null;
}
}
class PartImpl1<V> implements Part<V> {
@Override
public V getId() {
return null;
}
}
既然 GOP 是 GOP extends GroupOfParts<V, PartDecorator<V, ? extends Part<V>>> 并且 GroupOfPartsDecorImpl 最后应该是 GroupOfParts<V, PartDecorator<V, Part<V>> 为什么会出现这个错误?
【问题讨论】:
-
你能给出确切的信息,包括行号吗?见minimal reproducible example
-
顶部给出错误信息,这里是第3行,这部分是“
GroupOfPartsDecorImpl<V>>getInstance()” -
Eclipse 状态
Bound mismatch: The type GroupOfPartsDecorImpl<V> is not a valid substitute for the bounded parameter <GOP extends GroupOfParts<V,PartDecorator<V,? extends Part<V>>>> of the type MainContainerGroupPartDecorator<V,GOP> -
您想编辑您的问题以添加此类信息;-)
标签: java generics type-bounds