简体   繁体   English

Java泛型 - 不在范围内

[英]Java generics - not within bounds

I have following sample that cannot compile and produces Error:() java: type argument GroupOfPartsDecorImpl<V> is not within bounds of type-variable GOP . 我有以下无法编译和生成Error:() java: type argument GroupOfPartsDecorImpl<V> is not within bounds of type-variable GOP示例Error:() java: type argument GroupOfPartsDecorImpl<V> is not within bounds of type-variable GOP Code is the following: 代码如下:

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;
    }
}

Since GOP is GOP extends GroupOfParts<V, PartDecorator<V, ? extends Part<V>>> 由于GOP是GOP extends GroupOfParts<V, PartDecorator<V, ? extends Part<V>>> GOP extends GroupOfParts<V, PartDecorator<V, ? extends Part<V>>> and GroupOfPartsDecorImpl should be in the end GroupOfParts<V, PartDecorator<V, Part<V>> why this error shows up? GOP extends GroupOfParts<V, PartDecorator<V, ? extends Part<V>>>和GroupOfPartsDecorImpl应该在最后GroupOfParts<V, PartDecorator<V, Part<V>>为什么会出现此错误?

The second generic parameter to GroupOfParts must be PartDecorator<V, ? extends Part<V>> GroupOfParts的第二个通用参数必须是PartDecorator<V, ? extends Part<V>> PartDecorator<V, ? extends Part<V>> . PartDecorator<V, ? extends Part<V>> And since generics are invariant by default there can be no deviation from this. 并且由于泛型默认不变,因此不会有偏差。 But GroupOfPartsDecorImpl uses PartDecorator<V, PartImpl1<V>> , which is not the same, so it doesn't compile. 但是GroupOfPartsDecorImpl使用PartDecorator<V, PartImpl1<V>> ,这是不一样的,因此它不能编译。

You can solve this by making the second parameter covariant in the declaration of MainContainerGroupPartDecorator : 您可以通过在MainContainerGroupPartDecorator的声明MainContainerGroupPartDecorator第二个参数协变来解决这个问题:

class MainContainerGroupPartDecorator<V, GOP extends GroupOfParts<V,
    ? extends PartDecorator<V, ? extends Part<V>>>> 
    extends BaseContainerGroupPartDecorator<V, GOP> {

(Basically changing PartDecorator<V, ? extends Part<V>> to ? extends PartDecorator<V, ? extends Part<V>> ) (基本上改变PartDecorator<V, ? extends Part<V>> ? extends PartDecorator<V, ? extends Part<V>>? extends PartDecorator<V, ? extends Part<V>>

The problem is your bounded wildcard in the 3rd level of your 2nd generic ? extends Part<V> 问题是你的第二个通用的第3级有界通配符? extends Part<V> ? extends Part<V> . ? extends Part<V> You may specify it by an additional generic P 您可以通过额外的通用P指定它

class MainContainerGroupPartDecorator<V, P extends Part<V>, GOP extends GroupOfParts<V, PartDecorator<V, P>>>
    extends BaseContainerGroupPartDecorator<V, GOP>
{
    public static <V> MainContainerGroupPartDecorator<V,PartImpl1<V>, GroupOfPartsDecorImpl<V>> getInstance() {
        return null;
    }
}

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

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