簡體   English   中英

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

[英]What is the purpose of lower bounded wildcard in Function.class?

在Java8的Function.class中,我們具有:

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 ? super允許返回的Function的輸入類型( V )與參數輸入類型不同。

例如,用? super編譯? super ? super版本,但不是備用版本。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM