这是一个简单的例子:按重量比较汽车。我将首先以文本形式描述问题,然后演示如果省略? extends 或? super,它会如何出错。我还展示了在每种情况下都可用的丑陋的部分解决方法。 如果你更喜欢代码而不是散文,直接跳到第二部分,它应该是不言自明的。
问题的非正式讨论
首先,逆变器? super T。
假设您有两个类Car 和PhysicalObject,这样Car extends PhysicalObject。现在假设您有一个扩展 Function<PhysicalObject, Double> 的函数 Weight。
如果声明是Function<T,U>,那么您不能重用函数Weight extends Function<PhysicalObject, Double> 来比较两辆汽车,因为Function<PhysicalObject, Double> 不符合Function<Car, Double>。但您显然希望能够按重量比较汽车。因此,逆变式? super T 是有意义的,因此Function<PhysicalObject, Double> 符合Function<? super Car, Double>。
现在是协变 ? extends U 声明。
假设您有两个类Real 和PositiveReal,这样PositiveReal extends Real,并假设Real 是Comparable。
假设您在前面示例中的函数Weight 实际上有一个更精确的类型Weight extends Function<PhysicalObject, PositiveReal>。如果keyExtractor 的声明是Function<? super T, U> 而不是Function<? super T, ? extends U>,您将无法利用PositiveReal 也是Real 的事实,因此两个PositiveReals 不能相互比较,即使它们实现了Comparable<Real>,也没有不必要的限制Comparable<PositiveReal>。
总结一下:通过声明Function<? super T, ? extends U>,Weight extends Function<PhysicalObject, PositiveReal> 可以替换为Function<? super Car, ? extends Real> 以使用Cars 比较Comparable<Real>。
我希望这个简单的例子能阐明为什么这样的声明是有用的。
代码:当? extends 或? super 被省略时的后果的完整枚举
这是一个可编译的示例,它系统地列举了如果我们省略 ? super 或 ? extends 可能出错的所有事情。此外,还显示了两个(丑陋的)部分解决方法。
import java.util.function.Function;
import java.util.Comparator;
class HypotheticComparators {
public static <A, B> Comparator<A> badCompare1(Function<A, B> f, Comparator<B> cb) {
return (A a1, A a2) -> cb.compare(f.apply(a1), f.apply(a2));
}
public static <A, B> Comparator<A> badCompare2(Function<? super A, B> f, Comparator<B> cb) {
return (A a1, A a2) -> cb.compare(f.apply(a1), f.apply(a2));
}
public static <A, B> Comparator<A> badCompare3(Function<A, ? extends B> f, Comparator<B> cb) {
return (A a1, A a2) -> cb.compare(f.apply(a1), f.apply(a2));
}
public static <A, B> Comparator<A> goodCompare(Function<? super A, ? extends B> f, Comparator<B> cb) {
return (A a1, A a2) -> cb.compare(f.apply(a1), f.apply(a2));
}
public static void main(String[] args) {
class PhysicalObject { double weight; }
class Car extends PhysicalObject {}
class Real {
private final double value;
Real(double r) {
this.value = r;
}
double getValue() {
return value;
}
}
class PositiveReal extends Real {
PositiveReal(double r) {
super(r);
assert(r > 0.0);
}
}
Comparator<Real> realComparator = (Real r1, Real r2) -> {
double v1 = r1.getValue();
double v2 = r2.getValue();
return v1 < v2 ? 1 : v1 > v2 ? -1 : 0;
};
Function<PhysicalObject, PositiveReal> weight = p -> new PositiveReal(p.weight);
// bad "weight"-function that cannot guarantee that the outputs
// are positive
Function<PhysicalObject, Real> surrealWeight = p -> new Real(p.weight);
// bad weight function that works only on cars
// Note: the implementation contains nothing car-specific,
// it would be the same for every other physical object!
// That means: code duplication!
Function<Car, PositiveReal> carWeight = p -> new PositiveReal(p.weight);
// Example 1
// badCompare1(weight, realComparator); // doesn't compile
//
// type error:
// required: Function<A,B>,Comparator<B>
// found: Function<PhysicalObject,PositiveReal>,Comparator<Real>
// Example 2.1
// Comparator<Car> c2 = badCompare2(weight, realComparator); // doesn't compile
//
// type error:
// required: Function<? super A,B>,Comparator<B>
// found: Function<PhysicalObject,PositiveReal>,Comparator<Real>
// Example 2.2
// This compiles, but for this to work, we had to loosen the output
// type of `weight` to a non-necessarily-positive real number
Comparator<Car> c2_2 = badCompare2(surrealWeight, realComparator);
// Example 3.1
// This doesn't compile, because `Car` is not *exactly* a `PhysicalObject`:
// Comparator<Car> c3_1 = badCompare3(weight, realComparator);
//
// incompatible types: inferred type does not conform to equality constraint(s)
// inferred: Car
// equality constraints(s): Car,PhysicalObject
// Example 3.2
// This works, but with a bad code-duplicated `carWeight` instead of `weight`
Comparator<Car> c3_2 = badCompare3(carWeight, realComparator);
// Example 4
// That's how it's supposed to work: compare cars by their weights. Done!
Comparator<Car> goodComparator = goodCompare(weight, realComparator);
}
}
相关链接
- Scala 中定义点协变和逆变的详细说明:How to check covariant and contravariant position of an element in the function?