What's the nearest substitute for a function pointer in Java?

前端 未结 22 1774
太阳男子
太阳男子 2020-11-22 15:32

I have a method that\'s about ten lines of code. I want to create more methods that do exactly the same thing, except for a small calculation that\'s going to change one li

22条回答
  •  不要未来只要你来
    2020-11-22 16:14

    Since Java8, you can use lambdas, which also have libraries in the official SE 8 API.

    Usage: You need to use a interface with only one abstract method. Make an instance of it (you may want to use the one java SE 8 already provided) like this:

    Function functionname = (inputvariablename) {
    ... 
    return outputinstance;
    }
    

    For more information checkout the documentation: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

提交回复
热议问题