作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道在 Java 数组中是协变的。例如:
Assume Dog is a subclass of Animal
In java the arrays are covariant making: Animal[] a supertype of Dog[]
But in java generic collections are not covariant such as:
ArrayList<Animal> is not a supertype of ArrayList<Dog>
最佳答案
我认为“动物 [] [是] 狗 [] 的父类(super class)型”是指 Animal[42]
实际上可能是 Dog
?如果是这样,那么答案是否定的。
在 Java 中,变量(包括数组元素)实际上是引用(想想指针)。
给定的
type Animal is tagged null record;
type Dog is new Animal with null record;
type Plain_Array is array (Positive range <>) of Animal;
Animals
.
type Class_Array is array (Positive range <>) of Animal'Class;
gnatmake -c -u -f covariant_arrays.ads
gcc -c covariant_arrays.ads
covariant_arrays.ads:8:59: unconstrained element type in array declaration
gnatmake: "covariant_arrays.ads" compilation error
Animal
和
Dog
对象大小不同)。你可以试试
type Access_Array is array (Positive range <>) of access Animal'Class;
AA : Access_Array := (1 => new Animal, 2 => new Dog);
Ada.Containers.Indefinite_Vectors
来避免很多悲伤。 .
关于arrays - Ada 中的数组是协变的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8144750/
在我的设置中,我试图有一个界面 Table继承自 Map (因为它主要用作 map 的包装器)。两个类继承自 Table - 本地和全局。全局的将有一个可变的映射,而本地的将有一个只有本地条目的映射。
Rust Nomicon 有 an entire section on variance除了关于 Box 的这一小节,我或多或少地理解了这一点和 Vec在 T 上(共同)变体. Box and Vec
我是一名优秀的程序员,十分优秀!