gpt4 book ai didi

java - 列表

转载 作者:IT老高 更新时间:2023-10-28 20:46:39 25 4
gpt4 key购买 nike

我有一个关于泛型的 Java 问题。我声明了一个通用列表:

List<? extends MyType> listOfMyType;

然后我尝试用某种方法实例化并将项目添加到该列表中:

listOfMyType = new ArrayList<MyType>();
listOfMyType.add(myTypeInstance);

其中 myTypeInstance 只是 MyType 类型的对象;它不会编译。它说:

The method add(capture#3-of ? extends MyType) in the type List<capture#3-of ? extends MyType> is not applicable for the arguments (MyType)

有什么想法吗?

最佳答案

您不能对 extends 进行“放置”。看Generics - Get and Put rule .

关于java - 列表<?扩展我的类型>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/918026/

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