代码之家  ›  专栏  ›  技术社区  ›  alwayscurious

链接功能接口-IntUnaryOperator与UnaryOperator

  •  6
  • alwayscurious  · 技术社区  · 7 年前

    UnaryOperator Function ,但不是 IntUnaryOperator 到同一函数的末尾。

    UnaryOperator <String> twoOfMe = s -> s + s;
    Function <String, Integer> convertMe = s -> Integer.parseInt (s);
    
    UnaryOperator <Integer> twiceMe = n -> 2*n;
    
    IntUnaryOperator doubleMe = n -> 2*n;
    
    int a = twoOfMe.andThen(convertMe).andThen(twiceMe).apply ("2");
    
    int b = twoOfMe.andThen(convertMe).andThen(doubleMe).apply ("2");
    

    int a twiceMe 但是 int b 不适用于 doubleMe .

    编辑: 它表示不兼容的类型。必需的int。找到java。lang.对象

    3 回复  |  直到 7 年前
        1
  •  6
  •   Eran    7 年前

    andThen(Function<? super R, ? extends V> after) 需要一个 Function 论点 UnaryOperator<Integer> 是的子接口 Function<Integer,Integer> ,匹配。 IntUnaryOperator 接口,所以 doubleMe 无法传递给 andThen .

        2
  •  2
  •   Dorian Gray    3 年前

    这是因为 IntUnaryOperator int <String, Integer> convertMe 正在返回 Integer ,而不是 整数

    你可以申报 ToIntFunction<String> convertMe = Integer::parseInt; ,然后你可以把它们锁起来。

    UnaryOperator<String> twoOfMe = s -> s + s;
    ToIntFunction<String> convertMe = Integer::parseInt;
    IntUnaryOperator doubleMe = n -> 2*n;
    int b = twoOfMe.andThen(convertMe).andThen(doubleMe).apply ("2");
    
        3
  •  0
  •   Danylo Zatorsky    7 年前

    IntUnaryOperator 类,您将看到它不扩展任何接口,另一方面 UnaryOperator 是否扩展 Function

    您提供的代码无法使用 气管插管器 因为它期望 作用 气管插管器 确实如此。