b**********h 发帖数: 419 | 1 感觉这些概念是编程课里某个重要Topic里的
有什么书可以系统学习这些概念的, 最好是Java的书,
谢谢 |
g****t 发帖数: 31659 | 2 都是洗脑用的商品。
【在 b**********h 的大作中提到】 : 感觉这些概念是编程课里某个重要Topic里的 : 有什么书可以系统学习这些概念的, 最好是Java的书, : 谢谢
|
c*********e 发帖数: 16335 | 3 immutable就是这个值不能变。
【在 b**********h 的大作中提到】 : 感觉这些概念是编程课里某个重要Topic里的 : 有什么书可以系统学习这些概念的, 最好是Java的书, : 谢谢
|
a******i 发帖数: 6 | 4 see below:
For example, with the function type
A -> B // functional notation
public B meth(A arg) // how this looks in Java
we have the following:
Let C be a subtype of A, and D be a subtype of B. Then the following is
valid:
B b = meth(new C()); // B >= B, C < A
Object o = meth(new C()); // Object > B, C < A
but the follwoing are invalid:
D d = meth(new A()); // because D < B
B b = meth(new Object()); // because Object > A
hence, to check whether a call of meth is valid, we must check
The expected return type is a supertype of the declared return type.
The actual argument type is a subtype of the declared argument type.
This is all well known and intuitive. By convention we say that the return
type of a function is covariant, and the argument type of a method is
contravariant.
With parameterized types, like List, we have it that the argument type is
invariant in languages like Java, where we have mutability. We can't say
that a list of C's is a list of A's, because, if it were so, we could store
an A in a list of Cs, much to the surprise of the caller, who assumes only
Cs in the list. However, in languages where values are immutable, like
Haskell, this is not a problem. Because the data we pass to functions cannot
be mutated, a list of C actually is a list of A if C is a subtype of A. (
Note that Haskell has no real subtyping, but has instead the related notion
of "more/less polymorphic" types.) |
n*w 发帖数: 3393 | 5 Java应该不支持covariance?除了c#, 主流的还有哪个?
【在 a******i 的大作中提到】 : see below: : For example, with the function type : A -> B // functional notation : public B meth(A arg) // how this looks in Java : we have the following: : Let C be a subtype of A, and D be a subtype of B. Then the following is : valid: : B b = meth(new C()); // B >= B, C < A : Object o = meth(new C()); // Object > B, C < A : but the follwoing are invalid:
|
T*******x 发帖数: 8565 | 6 这个道理我还是没明白:如果C是A的subtype,为什么在Java中,list of C里不能存一
个type A的object,而在haskell里就可以。
【在 a******i 的大作中提到】 : see below: : For example, with the function type : A -> B // functional notation : public B meth(A arg) // how this looks in Java : we have the following: : Let C be a subtype of A, and D be a subtype of B. Then the following is : valid: : B b = meth(new C()); // B >= B, C < A : Object o = meth(new C()); // Object > B, C < A : but the follwoing are invalid:
|
n*w 发帖数: 3393 | 7 Java的list中又进又出。haskell只出
【在 T*******x 的大作中提到】 : 这个道理我还是没明白:如果C是A的subtype,为什么在Java中,list of C里不能存一 : 个type A的object,而在haskell里就可以。
|
T*******x 发帖数: 8565 | 8 那为什么只出就可以呢?
【在 n*w 的大作中提到】 : Java的list中又进又出。haskell只出
|
n*w 发帖数: 3393 | 9 这样的话就是sub type关系了。
【在 T*******x 的大作中提到】 : 那为什么只出就可以呢?
|
T*******x 发帖数: 8565 | 10 list(C) 是 list(A) 的 sub type?
这样的话就是sub type关系了。
【在 n*w 的大作中提到】 : 这样的话就是sub type关系了。
|
|
|
n*w 发帖数: 3393 | 11 yes, 反直观的是contravariant.
【在 T*******x 的大作中提到】 : list(C) 是 list(A) 的 sub type? : : 这样的话就是sub type关系了。
|
T*******x 发帖数: 8565 | 12 谢谢。这个地方我还是没想明白,但是我不知道该问什么问题好了。
【在 n*w 的大作中提到】 : yes, 反直观的是contravariant.
|
h**********c 发帖数: 4120 | 13 看过一个讲座,忘了,
我理解covariant是change the ceiling,contravariante是有一个floor.
实际中没用过,不过JEE里好多吗喜欢这么用。 |
n*w 发帖数: 3393 | 14 想到一个contravariant解释,假如有一个function eat,接受一个参数,类型为鱼。
那么吃动物应该是其子类。大吃一鲸不是。 |
l******t 发帖数: 55733 | 15 这是oo继承固有的问题。fp没这个问题。fp的category是mixin的。 |