协方差与放大器之间的差异;孔特拉方差 [英] Difference between Covariance & Contra-variance

查看:175
本文介绍了协方差与放大器之间的差异;孔特拉方差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解协变和逆变的区别。

I am having trouble understanding the difference between covariance and contravariance.

推荐答案

现在的问题是什么是协变和逆变的区别吗?

The question is "what is the difference between covariance and contravariance?"

协变和逆变是属性的的关联与其他一组的一个成员的映射函数。更具体地,映射可以是协变或逆变相对于关系的上组

Covariance and contravariance are properties of a mapping function that associates one member of a set with another. More specifically, a mapping can be covariant or contravariant with respect to a relation on that set.

考虑该组所有的C#类型的以下两个子集。第一:

Consider the following two subsets of the set of all C# types. First:

{ Animal, 
  Tiger, 
  Fruit, 
  Banana }.

第二,这显然与集:

And second, this clearly related set:

{ IEnumerable<Animal>, 
  IEnumerable<Tiger>, 
  IEnumerable<Fruit>, 
  IEnumerable<Banana> }

有一个的映射的从第一组到第二组操作。也就是说,每个吨的第一盘的对应的的在第二盘类型为的IEnumerable&LT; T&GT; 。或者,在短表,映射 T→IE&LT; T&GT;

There is a mapping operation from the first set to the second set. That is, for each T in the first set, the corresponding type in the second set is IEnumerable<T>. Or, in short form, the mapping is T → IE<T>.

随着我这么远吗?

现在让我们考虑一个的关系的。有一个在第一组的的分配兼容性关系的对类型之间。类型的值可分配给类型的变量动物,所以这些类型的被说成是分配兼容。让我们写类型的值 X 可分配给类型的变量在较短的形式:< code系列> X⇒是。因此,在我们的第一个子集,这里有所有的分配兼容性关系:

Now let's consider a relation. There is an assignment compatibility relationship between pairs of types in the first set. A value of type Tiger can be assigned to a variable of type Animal, so these types are said to be "assignment compatible". Let's write "a value of type X can be assigned to a variable of type Y" in a shorter form: X ⇒ Y. So in our first subset, here are all the assignment compatibility relationships:

Tiger  ⇒ Tiger
Tiger  ⇒ Animal
Animal ⇒ Animal
Banana ⇒ Banana
Banana ⇒ Fruit
Fruit  ⇒ Fruit

在C#4,它支持某些接口的协变分配兼容性,有一个在第二组对的类型之间的分配兼容性关系:

In C# 4, which supports covariant assignment compatibility of certain interfaces, there is an assignment compatibility relationship between pairs of types in the second set:

IE<Tiger>  ⇒ IE<Tiger>
IE<Tiger>  ⇒ IE<Animal>
IE<Animal> ⇒ IE<Animal>
IE<Banana> ⇒ IE<Banana>
IE<Banana> ⇒ IE<Fruit>
IE<Fruit>  ⇒ IE<Fruit>

注意,映射 T→IE&LT; T&GT; preserves的存在和分配兼容性的方向的。也就是说,如果 X⇒是,那么它也确实 IE&LT; X&GT; ⇒IE&LT; Y方式&gt;

Notice that the mapping T → IE<T> preserves the existence and direction of assignment compatibility. That is, if X ⇒ Y, then it is also true that IE<X> ⇒ IE<Y>.

具有这种属性相对于一特定的关系的映射被称为协变映射。这应该是有意义:可以在需要的动物的序列中使用老虎的序列,但相反的是不正确的。动物的序列可以在需要老虎的顺序不一定使用。

A mapping which has this property with respect to a particular relation is called a "covariant mapping". This should make sense: a sequence of Tigers can be used where a sequence of Animals is needed, but the opposite is not true. A sequence of animals cannot necessarily be used where a sequence of Tigers is needed.

这就是协方差。现在考虑集合所有类型的子集:

That's covariance. Now consider this subset of the set of all types:

{ IComparable<Tiger>, 
  IComparable<Animal>, 
  IComparable<Fruit>, 
  IComparable<Banana> }

现在我们从第一套到第三套映射 T→IC标签; T&GT;

now we have the mapping from the first set to the third set T → IC<T>.

在C#4:

IC<Tiger>  ⇒ IC<Tiger>
IC<Animal> ⇒ IC<Tiger>     Backwards!
IC<Animal> ⇒ IC<Animal>
IC<Banana> ⇒ IC<Banana>
IC<Fruit>  ⇒ IC<Banana>     Backwards!
IC<Fruit>  ⇒ IC<Fruit>

也就是说,映射 T→IC标签; T&GT; 拥有的 preserved的存在,但扭转分配兼容性的方向的。也就是说,如果 X⇒是,那么 IC标签; X&GT; ⇐IC标签; Y方式&gt;

That is, the mapping T → IC<T> has preserved the existence but reversed the direction of assignment compatibility. That is, if X ⇒ Y, then IC<X> ⇐ IC<Y>.

其中的的映射preserves但挫折的关系被称为逆变的映射。

A mapping which preserves but reverses a relation is called a contravariant mapping.

此外,这应该是显然是正确的。它可以比较两个动物的设备也可以比较两只老虎,但可以比较两个老虎的设备不一定能比较任意两个动物。

Again, this should be clearly correct. A device which can compare two Animals can also compare two Tigers, but a device which can compare two Tigers cannot necessarily compare any two Animals.

这就是在C#4方差协变和逆变的区别的 preserves 的转让的方向。逆变的逆转的吧。

So that's the difference between covariance and contravariance in C# 4. Covariance preserves the direction of assignability. Contravariance reverses it.

这篇关于协方差与放大器之间的差异;孔特拉方差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆