java java泛型协方差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2660827/
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-10-29 22:15:49  来源:igfitidea点击:

java generics covariance

javagenericsaliascovariance

提问by soocracy42

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

我无法理解以下文章: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.

因为 ln 是一个 List,所以向它添加一个 Float 似乎是完全合法的。但是如果 ln 与 li 作别名,那么它会破坏 li 定义中隐含的类型安全承诺——它是一个整数列表,这就是泛型类型不能协变的原因。

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.

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

回答by bakkal

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.

在 Java 中, Integer 继承自 Number (java.lang.Number),所以直观地说,任何是 Integer 的东西(java.lang.Integer)也是一个数字,但是那篇文章指出的是,对于泛型它不能那样工作,因为考虑到这个例子,你最终可能会放置一个浮点数(这是一个数字)转换为 a List<Integer>,这是非法的,因为浮点数不是整数。

Conclusion:Generics are not covariant.

结论:泛型不是协变的。

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

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

回答by TofuBeer

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.

事情会很糟糕。在这个例子中,bar 是 foo 的别名,如果你能做到,你将失去类型安全性,这是泛型存在的主要原因。

回答by jai prakash

public class vechicle {
void drive(){
}
}
class car extends vechicle{
        //Covariance
    vechicle getObject(){
        return new car();
    }
        //contravariance
    car getmyObject(){
        return (car) new vechicle(); 
    }
}