我正在尝试使用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 Function<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);
您可以使用一种方法:
abstract Function<Integer, Integer> overriden();
final Function<Integer, Integer> pipe = sourceInt
.andThen(timesTwo)
.andThen(overriden())
.andThen(addOne)
.andThen(addTwo);