java泛型协方差

发布于 2024-08-30 00:00:15 字数 553 浏览 2 评论 0原文

我无法理解以下文章: http: //www.ibm.com/developerworks/java/library/j-jtp01255.html

下,

泛型不是协变的

作者指出,

因为ln是一个List,所以添加一个 浮动似乎完全合法。但 如果 ln 是 li 的别名,那么它 会违反类型安全承诺 隐含在 li 的定义中—— 它是一个整数列表,其中 这就是为什么泛型类型不能 协变。

我无法理解它所说的部分 “如果 ln 是 li 的别名”。作者所说的别名是什么意思?(参考?)。引用行上方的代码片段似乎说明了 java 中什么是非法的,而不是为什么。如果有人能用一个例子来解释,这对我会非常有帮助。 提前致谢。

I am having trouble understanding the following article:
http://www.ibm.com/developerworks/java/library/j-jtp01255.html

Under,

Generics are not covariant

the author states,

Because ln is a List, adding a
Float to it seems perfectly legal. But
if ln were aliased with li, then it
would break the type-safety promise
implicit in the definition of li --
that it is a list of integers, which
is why generic types cannot be
covariant.

I can't understand the part where it says
"if ln were aliased with li". What does the author means by alias?(reference?). The code snippet above the quoted line seems to illustrate WHAT is illegal in java and not WHY. It would be very helpful to me if somebody could explain with an example.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

篱下浅笙歌 2024-09-06 00:00:15
List<Integer> li = new ArrayList<Integer>();
List<Number> ln = li; // illegal
ln.add(new Float(3.1415));

在 Java 中,Integer 继承自 Number(java.lang.Number),因此直观上来说,任何 Integer(java.lang.Integer) 也是数字,但是该文章指出的是,对于泛型,它不会以这种方式工作,因为考虑到该示例,您最终可能会将浮点数(它是数字)放入 List 中,即非法,因为浮点数不是整数。

结论:泛型不是协变的。

注意:我建议您阅读《Effective Java(第二版)》第 5 章:泛型。

List<Integer> li = new ArrayList<Integer>();
List<Number> ln = li; // illegal
ln.add(new Float(3.1415));

In Java, Integer inherits from Number(java.lang.Number), so intuitively, anything that is an Integer(java.lang.Integer) is also a number, but what that article points out is that with generics it does not work that way, because considering that example, you could end up putting a float (which is a Number) into a List<Integer>, which is illegal because a float is not an integer.

Conclusion: Generics are not covariant.

Note: I recommend you read Effective Java (2nd Edition) Chapter 5: Generics.

揪着可爱 2024-09-06 00:00:15

如果你能做这样的事情:

List<Float> foo;
List<Object> bar;

foo = new ArrayList<Float>();
bar = foo;

foo.add(1.0f);
bar.add("Hello");

事情就会变得非常糟糕。在此示例中,bar 是 foo 的别名,如果您可以这样做,您将失去类型安全性,而类型安全性是泛型存在的主要原因。

If you could do something like this:

List<Float> foo;
List<Object> bar;

foo = new ArrayList<Float>();
bar = foo;

foo.add(1.0f);
bar.add("Hello");

things would go VERY wrong. In this example bar is an alias for foo, and if you could do it you would lose the type safety that are the main reason that generics exist.

二货你真萌 2024-09-06 00:00:15
public class vechicle {
void drive(){
}
}
class car extends vechicle{
        //Covariance
    vechicle getObject(){
        return new car();
    }
        //contravariance
    car getmyObject(){
        return (car) new vechicle(); 
    }
}
public class vechicle {
void drive(){
}
}
class car extends vechicle{
        //Covariance
    vechicle getObject(){
        return new car();
    }
        //contravariance
    car getmyObject(){
        return (car) new vechicle(); 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文