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);