gpt4 book ai didi

Java 泛型和有界类型

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

我有一个 ConcurrentMap 的包装类,如下所示:

public MapWrapper<K, V> implements ConcurrentMap<K, V> {

private final ConcurrentMap<K, V> wrappedMap;

...
@Override
public void putAll(Map<? extends K, ? extends V> map) {
wrappedMap.putAll(map); // <--- Gives compilation error
}
...
}

标记的行会触发以下编译错误:

method putAll in interface java.util.Map<K,V> cannot be applied to given types;
required: java.util.Map<? extends capture#5 of ? extends K,? extends capture#6 of ?
extends V>
found: java.util.Map<capture#7 of ? extends K,capture#8 of ? extends V>
reason: actual argument java.util.Map<capture#7 of ? extends K,capture#8 of ? extends V>
cannot be converted to java.util.Map<? extends capture#5 of ? extends K,? extends
capture#6 of ? extends V> by method invocation conversion

我怀疑无限通配符是罪魁祸首,但我无法更改方法签名,因为它是从 ConcurrentMap 接口(interface)继承的。有任何想法吗?

最佳答案

关于Java 泛型和有界类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6239413/

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