【问题标题】:Java generics - not within boundsJava 泛型 - 不在界限内
【发布时间】: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&lt;V, PartDecorator&lt;V, ? extends Part&lt;V&gt;&gt;&gt; 并且 GroupOfPartsDecorImpl 最后应该是 GroupOfParts&lt;V, PartDecorator&lt;V, Part&lt;V&gt;&gt; 为什么会出现这个错误?

【问题讨论】:

  • 你能给出确切的信息,包括行号吗?见minimal reproducible example
  • 顶部给出错误信息,这里是第3行,这部分是“GroupOfPartsDecorImpl&lt;V&gt;>getInstance()”
  • Eclipse 状态 Bound mismatch: The type GroupOfPartsDecorImpl&lt;V&gt; is not a valid substitute for the bounded parameter &lt;GOP extends GroupOfParts&lt;V,PartDecorator&lt;V,? extends Part&lt;V&gt;&gt;&gt;&gt; of the type MainContainerGroupPartDecorator&lt;V,GOP&gt;
  • 您想编辑您的问题以添加此类信息;-)

标签: java generics type-bounds


【解决方案1】:

GroupOfParts 的第二个通用参数必须是 PartDecorator&lt;V, ? extends Part&lt;V&gt;&gt;。而且由于泛型默认情况下是不变的,因此不能偏离这一点。但是GroupOfPartsDecorImpl使用PartDecorator&lt;V, PartImpl1&lt;V&gt;&gt;,不一样,所以编译不了。

你可以通过在MainContainerGroupPartDecorator的声明中使第二个参数协变来解决这个问题:

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

(基本上把PartDecorator&lt;V, ? extends Part&lt;V&gt;&gt;改成? extends PartDecorator&lt;V, ? extends Part&lt;V&gt;&gt;

【讨论】:

  • 你能解释一下吗? 并且由于泛型默认情况下是不变的,因此不会有任何偏差
  • @zencv 我会参考这个问题:stackoverflow.com/questions/2745265/…
  • 这似乎解决了问题。谢谢。
  • @JornVernee 一个不相关的问题:是否有可能有这种规范类SubMainContainerGroupPartDecorator&lt;V, P extends Part&lt;V&gt;&gt; extends BaseContainerGroupPartDecorator&lt;V, ? extends GroupOfParts&lt;V, ? extends PartDecorator&lt;V, P&gt;&gt;&gt; { -> 所以改为指定 GroupOfParts 我指定 Part
  • @bojanv55 不,超类型不能使用任何通配符。
【解决方案2】:

问题是您在第二个通用 ? extends Part&lt;V&gt; 的第三级中的有界通配符。您可以通过额外的通用 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;
    }
}

【讨论】:

  • 我已经用这种方式修复了它,但我希望有比这样做更简单的方法......
猜你喜欢
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-16
  • 1970-01-01
相关资源
最近更新 更多