Java 列表<? 扩展我的类型>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/918026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:03:37  来源:igfitidea点击:

List<? extends MyType>

javalistgenericsbounded-wildcard

提问by Lancelot

I have a Java question about generics. I declared a generic list:

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

List<? extends MyType> listOfMyType;

Then in some method I try instantiate and add items to that list:

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

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

Where myTypeInstanceis just an object of type MyType; it won't compile. It says:

WheremyTypeInstance只是一个类型的对象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)

类型 List<capture#3-of 中的方法 add(capture#3-of ? extends MyType) extends MyType> 不适用于参数 (MyType)

Any idea?

任何的想法?

采纳答案by Surya

You cannot do a "put" with extends . Look at Generics - Get and Put rule.

你不能用 extends 做一个“放置”。查看泛型-获取和放置规则

回答by Cameron Pope

You shouldn't need to use the wildcard capture syntax in your case, simply declaring

您不需要在您的案例中使用通配符捕获语法,只需声明

List<MyType> listOfMytype;

should be enough. If you want to know exactly why, the Java Generics Tutorialhas more than you would ever want to know about the esoteric craziness of Java Generics. Page 20 addresses your specific case.

应该够了。如果您想确切地知道原因,Java 泛型教程中包含的关于 Java 泛型的深奥疯狂的内容比您想知道的要多。第 20 页针对您的具体情况。

As for why add with the wildcard capture does not work, it is because the compiler can't determine exactly what subclass of MyType the list will be in every case, so the compiler emits an error.

至于为什么用通配符捕获add不起作用,是因为编译器无法准确判断每种情况下列表会是MyType的哪个子类,所以编译器会报错。

回答by Tom Hawtin - tackline

Consider:

考虑:

class MySubType extends MyType {
}

List<MySubType> subtypeList = new ArrayList<MySubType>();
List<? extends MyType> list = subtypeList;
list.add(new MyType());
MySubType sub = subtypeList.get(0);

subnow contains a MyTypewhich is very wrong.

sub现在包含一个MyType非常错误的。

回答by zslevi

There is a similar thread here: How can elements be added to a wildcard generic collection?

这里有一个类似的线程: 如何将元素添加到通配符泛型集合中?

To get an idea of how generics works check out this example:

要了解泛型的工作原理,请查看以下示例:

    List<SubFoo> sfoo = new ArrayList<SubFoo>();
    List<Foo> foo;
    List<? extends Foo> tmp;

    tmp = sfoo;
    foo = (List<Foo>) tmp;

The thing is, that wasn't designed for local/member variables, but for function signatures, that's why it's so ass-backwards.

问题是,这不是为局部/成员变量设计的,而是为函数签名设计的,这就是为什么它如此落后。

回答by will824

I dont know if this will really help you, but this is something I had to use while calling a generic method of Spring Framework and wanting to return also a generic list:

我不知道这是否真的对你有帮助,但这是我在调用 Spring Framework 的泛型方法并希望返回泛型列表时必须使用的东西:

public <T> List<T> findAll(String tableName,Class<?> table) {
    String sql = "SELECT * FROM "+ tableName ;
    List<?> entities = getSimpleJdbcTemplate().query(sql,
            ParameterizedBeanPropertyRowMapper.newInstance(table));
            return (List<T>) entities;
}

Seems the parametrization needs you to use the ? sign in the list to receive the results and then cast the list to the expected return type.

似乎参数化需要您使用 ? 登录列表以接收结果,然后将列表转换为预期的返回类型。

Iam still dazzled by generics...

我仍然对泛型感到眼花缭乱......