函数.class中下界通配符的目的是什么?



In Function.class 从 Java8 中,我们有:

default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
    Objects.requireNonNull(before);
    return (V v) -> apply(before.apply(v));
}

撰写接受:

Function<? super V, ? extends T> before

而不是:

Function<V, ? extends T> before

是否存在"V"是下限这一事实的合理情况?

? super允许返回Function的输入类型(V)与参数输入类型不同。

例如,这会使用 ? super 版本进行编译,但不会使用备用版本进行编译。

Function<Object, String> before = Object::toString;
Function<String, Integer> after = Integer::parseInt;
Function<Integer, Integer> composed = after.compose(before);

最新更新