我正在尝试使用Java 8实现管道设计模式,并提供以下文章供我参考:

https://stackoverflow.com/a/58713936/4770397

码:

public abstract class Pipeline{
Function<Integer, Integer> addOne = it -> {
        System.out.println(it + 1);
        return it + 1;
    };

Function<Integer, Integer> addTwo = it -> {
        System.out.println(it + 2);
        return it + 2;
    };

Function<Integer, Integer> timesTwo = input -> {
        System.out.println(input * 2);
        return input * 2;
    };

final Function<Integer, Integer> pipe = sourceInt
    .andThen(timesTwo)
    .andThen(addOne)
    .andThen(addTwo);
}

我正在尝试添加一种抽象方法并想覆盖它。
我正在尝试做类似的事情:
abstract BiFunction<Integer, Integer,Integer> overriden;

并将管道更改为:
final Function<Integer, Integer> pipe = sourceInt
    .andThen(timesTwo)
    .andThen(overriden)
    .andThen(addOne)
    .andThen(addTwo);
}

但是问题是,我不知道将Function<Integer, Integer>声明为抽象方法。

最佳答案

您可以只声明一个常规的抽象方法,该方法采用Integer并返回Integer并使用方法引用语法对其进行引用:

public abstract Integer overridden(Integer input);

final Function<Integer, Integer> pipe = sourceInt
    .andThen(timesTwo)
    .andThen(this::overridden)
    .andThen(addOne)
    .andThen(addTwo);

10-08 08:33
查看更多