【问题标题】:How to create abstract Function using java 8如何使用 java 8 创建抽象函数
【发布时间】:2020-03-29 12:59:15
【问题描述】:

我正在尝试使用 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&lt;Integer, Integer&gt; 声明为抽象方法。

【问题讨论】:

    标签: java java-8 functional-interface


    【解决方案1】:

    您可以只声明一个常规抽象方法,该方法接受 Integer 并返回 Integer 并使用方法引用语法来引用它:

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

    【讨论】:

    • 感谢您的回答,我如何传递 2 个参数而不是 1 个参数?
    • @mayankbisht 你在找BiFunction吗?
    • @Sweeper- 是的,我正在寻找 BiFunction。
    • @mayankbisht 一直以来,当您编写类似于Function&lt;Integer, Integer&gt; timesTwo = input -&gt; {...}; 的行时,这表示接受一个参数,而问题从未提及有关传递 2 个参数的任何内容。请务必对您打算提出的问题进行编辑,否则问题只会丢失。
    • @Naman - 谢谢你的建议。我已经更新了问题。
    【解决方案2】:

    字段不受多态性影响 - 您需要一种方法。你要么写

    1. 只返回Function&lt;Integer, Integer&gt; 实例的方法

      public abstract Function<Integer, Integer> getMyFunction();
      
    2. 一个实际代表Function&lt;Integer, Integer&gt;的方法

      public abstract Integer myFunction(Integer i);
      

    然后做

    .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
      }
    }
    

    【讨论】:

    • 感谢您的回答,我如何传递 2 个参数而不是 1 个参数?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 2016-07-21
    相关资源
    最近更新 更多