如何使用Java8创建抽象函数
我正在尝试使用Java 8实现一个管道设计模式,下面的文章供我参考: 代码:如何使用Java8创建抽象函数,java,java-8,functional-interface,Java,Java 8,Functional Interface,我正在尝试使用Java 8实现一个管道设计模式,下面的文章供我参考: 代码: 公共抽象类管道{ 函数addOne=it->{ 系统输出打印项次(it+1); 返回+1; }; 函数addTwo=it->{ 系统输出打印项次(it+2); 返回+2; }; 功能时间2=输入->{ 系统输出打印项次(输入*2); 返回输入*2; }; 最终函数管道=sourceInt .第二次(第二次) .然后(加一) .第二(加两); } 我正在尝试添加一个抽象方法并希望覆盖它。 我正在尝试做一些类似的事情:
公共抽象类管道{
函数addOne=it->{
系统输出打印项次(it+1);
返回+1;
};
函数addTwo=it->{
系统输出打印项次(it+2);
返回+2;
};
功能时间2=输入->{
系统输出打印项次(输入*2);
返回输入*2;
};
最终函数管道=sourceInt
.第二次(第二次)
.然后(加一)
.第二(加两);
}
我正在尝试添加一个抽象方法并希望覆盖它。我正在尝试做一些类似的事情:
abstract BiFunction<Integer, Integer,Integer> overriden;
覆盖抽象双函数;
并将管道更改为:final Function<Integer, Integer> pipe = sourceInt
.andThen(timesTwo)
.andThen(overriden)
.andThen(addOne)
.andThen(addTwo);
}
final Function pipe=sourceInt
.第二次(第二次)
.然后(覆盖)
.然后(加一)
.第二(加两);
}
但问题是,我不知道如何将
函数
声明为抽象方法。您可以声明一个常规的抽象方法,该方法接受一个整数
并返回一个整数
,然后使用方法引用语法引用它:
public abstract Integer overridden(Integer input);
final Function<Integer, Integer> pipe = sourceInt
.andThen(timesTwo)
.andThen(this::overridden)
.andThen(addOne)
.andThen(addTwo);
覆盖公共抽象整数(整数输入);
最终函数管道=sourceInt
.第二次(第二次)
.然后(此::已覆盖)
.然后(加一)
.第二(加两);
字段不受多态性的影响-您需要一个方法。你要么写信
函数
实例的方法
public abstract Function<Integer, Integer> getMyFunction();
.andThen(getMyFunction())
或
分别
比如说,
interface I {
Function<Integer, Integer> getFunction();
Integer function(Integer i);
}
class A implements I {
@Override
public Function<Integer, Integer> getFunction() {
return i -> i + 1;
}
@Override
public Integer function(Integer i) {
return i + 1;
}
}
class B implements I {
@Override
public Function<Integer, Integer> getFunction() {
return i -> i * 2;
}
@Override
public Integer function(Integer i) {
return i * 2;
}
}
class T {
public static void main(String[] args) {
A a = new A();
B b = new B();
Function<Integer, Integer> f1 = a.getFunction().andThen(b.getFunction());
System.out.println(f1.apply(2)); // 6
Function<Integer, Integer> f2 = a.getFunction().andThen(b::function);
System.out.println(f2.apply(2)); // 6
Function<Integer, Integer> f3 = ((Function<Integer, Integer>)a::function).andThen(b::function);
System.out.println(f3.apply(2)); // 6
}
}
接口I{
函数getFunction();
整数函数(整数i);
}
A级执行器I{
@凌驾
公共函数getFunction(){
返回i->i+1;
}
@凌驾
公共整数函数(整数i){
返回i+1;
}
}
B类工具I{
@凌驾
公共函数getFunction(){
返回i->i*2;
}
@凌驾
公共整数函数(整数i){
返回i*2;
}
}
T类{
公共静态void main(字符串[]args){
A=新的A();
B=新的B();
函数f1=a.getFunction(),然后是(b.getFunction());
System.out.println(f1.apply(2));//6
函数f2=a.getFunction(),然后(b::Function);
System.out.println(f2.apply(2));//6
函数f3=((函数)a::Function)。然后(b::Function);
System.out.println(f3.apply(2));//6
}
}
谢谢你的回答,而不是1个参数。我怎样才能传递2个参数?谢谢你的回答,而不是1个参数。我怎样才能传递2个参数?@mayankbisht你在找吗?@Sweeper-是的,我在找双函数。@mayankbisht一直在写类似于函数times2=input->{…}的行代码>,表示接受一个参数,而问题从未提及任何有关传递两个参数的内容。一定要对你想问的问题进行编辑,否则问题只会丢失。@Naman-谢谢你的建议。我已经更新了问题。
.andThen(getMyFunction())
.andThen(i -> myFunction(i))
// .andThen(this::myFunction) // fancier
interface I {
Function<Integer, Integer> getFunction();
Integer function(Integer i);
}
class A implements I {
@Override
public Function<Integer, Integer> getFunction() {
return i -> i + 1;
}
@Override
public Integer function(Integer i) {
return i + 1;
}
}
class B implements I {
@Override
public Function<Integer, Integer> getFunction() {
return i -> i * 2;
}
@Override
public Integer function(Integer i) {
return i * 2;
}
}
class T {
public static void main(String[] args) {
A a = new A();
B b = new B();
Function<Integer, Integer> f1 = a.getFunction().andThen(b.getFunction());
System.out.println(f1.apply(2)); // 6
Function<Integer, Integer> f2 = a.getFunction().andThen(b::function);
System.out.println(f2.apply(2)); // 6
Function<Integer, Integer> f3 = ((Function<Integer, Integer>)a::function).andThen(b::function);
System.out.println(f3.apply(2)); // 6
}
}