【问题标题】:Instanciate a vector of a type with subtype [duplicate]实例化具有子类型的类型的向量
【发布时间】:2015-05-29 18:32:03
【问题描述】:

如果我们有这些类:

abstract class Figure{
}

class Cercle extends Figure{    
}

如果我写:

Vector<Figure> v = new Vector<Cercle>();

我有错误:

incompatible types: Vector<Cercle> cannot be converted to Vector<Figure>

如果有人知道它是如何不起作用的,我很想知道为什么。谢谢你,对不起我的英语。

【问题讨论】:

标签: java object instance-variables


【解决方案1】:

多次回答。 Java 中的泛型类型不是协变的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    相关资源
    最近更新 更多