上限通配符导致Java中的编译错误 [英] Upper bounded wildcards causing compilation error in Java

查看:128
本文介绍了上限通配符导致Java中的编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么会出现这些编译错误:

I cannot understand why I am getting these compilation errors:

1:


列表类型中的方法add(capture#1-of扩展Exec.Bird)不适用于参数(Exec.Sparrow)

The method add(capture#1-of ? extends Exec.Bird) in the type List is not applicable for the arguments (Exec.Sparrow)

2:


列表类型中的add(capture#2-of?extended Exec.Bird)方法是不适用于参数(Exec.Bird)

The method add(capture#2-of ? extends Exec.Bird) in the type List is not applicable for the arguments (Exec.Bird)



static class Bird{}
static class Sparrow extends Bird{}

public static void main(String[] args){
    List<? extends Bird> birds = new ArrayList<Bird>();
    birds.add(new Sparrow()); //#1 DOES NOT COMPILE
    birds.add(new Bird());// //#2 DOES NOT COMPILE
}


推荐答案

具有 List< ;?扩展了Bird> ,您实际上说的是 Bird子类型的任何类型 1 。这与说每种扩展 Bird 的类型不同。

With List<? extends Bird> you actually say any type that is a subtype of Bird1. It's not the same as saying every type that extends Bird.

可以是 Sparrow ,但也可以是 Blackbird 。如果您尝试将麻雀添加到可能仅包含 Blackbird 的列表中,它不会工作。出于相同的原因,您不能将 Bird 添加到可能 Sparrow s。

That means that ? can be a Sparrow, but it can also be a Blackbird. If you try to add a Sparrow to a list that could contain only Blackbirds, it won't work. For that same reason you cannot add a Bird to a list that could be a List of Sparrows.

为了使工作正常,您只需将列表的声明更改为:

In order to make things work, you just change the declaration of the list to this:

List<Bird> birds = new ArrayList<>();

或使用下限:

List<? super Bird> birds = new ArrayList<>();

关于这个下限示例:声明实际上说 Bird 或它的超类之一 。这意味着您可以安全地添加麻雀 Bird ,因为两者都符合这些条件。

About this lower bound example: the declaration actually says any type that is a Bird or one of its superclasses. That means that you can safely add a Sparrow or a Bird, because both meet those criteria.

通常来说,您应该使用吗?超级... ,当您写入列表时,?从列表中读取时,扩展... 。如果您同时阅读和写作,则不应使用边界。

Generally speaking, you should use ? super ... when you are writing to the list, and ? extends ... when you are reading from the list. If you are both reading and writing, you shouldn't use bounds.

此答案提供了有关泛型的非常有用的信息。

This answer provides very useful information about generics. You definitely should read it.

1 我认为以下陈述更加准确: 一个未知但特定的类型,它是 Bird 的子类型。

1 I think the following statement is even more accurate: "an unknown, but specific type that is a subtype of Bird".

这篇关于上限通配符导致Java中的编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆