gpt4 book ai didi

java - 不兼容类型 : SomeX cannot be converted to CAP#1

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:52:29 27 4
gpt4 key购买 nike

<分区>

我不明白这里出了什么问题

import java.util.*;

class Main {
private static class SomeX {}

void doSomethingWithX(SomeX someX) {
Collection<? extends SomeX> colectionOfX = new ArrayList<>();
colectionOfX.add(someX);
}
}

javac 表示如下:

  Main.java:8: error: method add in interface Collection<E> cannot be applied to given types;
colectionOfX.add(someX);
^
required: CAP#1
found: SomeX
reason: argument mismatch; SomeX cannot be converted to CAP#1
where E is a type-variable:
E extends Object declared in interface Collection
where CAP#1 is a fresh type-variable:
CAP#1 extends SomeX from capture of ? extends SomeX

根据我的理解,extends 将 CAP#1 的下限限制为 SomeX 并且 SomeX 本身应该满足该限制。

怎么了? (使用 javac 1.8.0_102 编译)

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