【问题标题】:Having Issue with Bounded Wildcards in Generic泛型中的有界通配符有问题
【发布时间】:2013-06-30 05:55:20
【问题描述】:

我是 Java 泛型的新手,我目前正在尝试使用泛型编码......最终目标是将旧的非泛型遗留代码转换为泛型......

我用 IS-A 定义了两个类,即一个是另一个的子类。

public class Parent {
    private String name;
    public Parent(String name) {
        super();
        this.name = name;
    }
}

public class Child extends Parent{
    private String address;
    public Child(String name, String address) {
        super(name);
        this.address = address;
    }
}

现在,我正在尝试使用有界通配符创建一个列表。并得到编译器错误。

List<? extends Parent> myList = new ArrayList<Child>(); 
myList.add(new Parent("name")); // compiler-error
myList.add(new Child("name", "address")); // compiler-error
myList.add(new Child("name", "address")); // compiler-error

有点困惑。请帮我看看这有什么问题?

【问题讨论】:

  • myList.add(new Parent("name")); // compiler-error 这不起作用,因为您声明了一个 Child 列表,但添加了一个 parent .. 它不具备成为 Child 所需的某些特征(在您的情况下为地址)。此外,@sanbhat 下面的回答可以解决您的问题。

标签: java generics collections


【解决方案1】:

那是因为你创建了ArrayList&lt;Child&gt;

为了达到同样的效果(即创建一个可以容纳Parent的所有子类的List),您可以简单地将其声明为List&lt;Parent&gt; myList = new ArrayList&lt;Parent&gt;();

List<Parent> myList = new ArrayList<Parent>(); --> new ArrayList should have generic type Parent
myList.add(new Parent("name")); // will work
myList.add(new Child("name", "address")); // will work
myList.add(new Child("name", "address")); // will work

编辑:

为了回答您的其他困惑,在Upper bound wild card 类型上写字是非法的,here's is a thread 解释为什么会这样。

【讨论】:

  • 抱歉声明列表无效。我已经更正了我的答案
【解决方案2】:

这就是编译错误的原因:

List<?> myList2 = new ArrayList<Child>(); 
myList2.add(new Child("name", "address")); // compiler-error

List<? extends Parent> myList2 = new ArrayList<Child>(); 
myList1.add(new Child("name", "address")); // compiler-error

由于我们不知道 myList2/myList1 的元素类型代表什么,我们无法向其中添加对象。 add() 方法接受类型 E 的参数,即集合的元素类型。当实际类型参数为 ? 时,它代表某种未知类型。我们传递给 add 的任何参数都必须是这种未知类型的子类型。因为我们不知道那是什么类型,所以我们不能传入任何东西。唯一的例外是 null,它是每个类型的成员。

另一方面,给定一个 List /列表,我们只能调用 get() 并使用结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    相关资源
    最近更新 更多