采用lambda表达式:

import java.util.function.Function;
class Test { public static void main(String... args) {
Function<Integer, Integer> increase = e -> e + 7; // lambda表达式 System.out.println(increase.getClass()); funcPlus(3, increase); } public static void funcPlus(int value, Function<Integer, Integer> func) {
System.out.println(func.apply(value));
} }

输出结果:

class com.classTest.Test$$Lambda$1/0x0000000801200840
10

(1)apply() 函数在最新的1.8 Java版本才支持

java.util.function
Interface Function<T,R>
Type Parameters:
T - the type of the input to the function
R - the type of the result of the function
All Known Subinterfaces:
UnaryOperator<T>
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. @FunctionalInterface
public interface Function<T,R>
Represents a function that accepts one argument and produces a result.
This is a functional interface whose functional method is apply(Object). Since:
1.8

API文档:https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html

采用Callable方式:

import java.util.concurrent.Callable;

public class CallableUse {
public static void main(String... args) { // final int num = 100770; or
int num = 100770;
// 使用匿名的内部类, 如果需要传递参数可能需要将变量转换成final:
try {
callMethod(100, new Callable<Integer>() {
public Integer call() {
return needOperation(num);
}
});
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} } public static int needOperation(int param) {
// do something
param = 999;
return param;
} public static void callMethod(int i, Callable<Integer> myFunc) {
// do something
try {
System.out.println(myFunc.call() );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

输出结果:

999

附:

同时在这篇帖子上有人采用Java反射机制:

https://stackoverflow.com/questions/4685563/how-to-pass-a-function-as-a-parameter-in-java

import java.lang.reflect.Method;

public class Demo {

    public static void main(String[] args) throws Exception{
Class[] parameterTypes = new Class[1];
parameterTypes[0] = String.class;
Method method1 = Demo.class.getMethod("method1", parameterTypes); Demo demo = new Demo();
demo.method2(demo, method1, "Hello World");
} public void method1(String message) {
System.out.println(message);
} public void method2(Object object, Method method, String message) throws Exception {
Object[] parameters = new Object[1];
parameters[0] = message;
method.invoke(object, parameters);
} }

参考资料:

https://techndeck.com/how-to-pass-function-as-a-parameter-in-a-method-in-java-8/

最新文章

  1. zookeeper+jstorm的集群搭建
  2. SVN使用说明
  3. overflow:hidden---清除浮动,隐藏溢出
  4. Linux平台下快速搭建FTP服务器
  5. [工具类]文件或文件夹xx已存在,则重命名为xx(n)(2)
  6. Python学习总结11:获取当前运行类名和函数名
  7. SQLServer中在视图上使用索引(转载)
  8. js学习笔记-编写高效、规范的js代码-Tom
  9. jquery validate如何不提交表单就做验证(ajax提交数据)
  10. 基于Heritrix的特定主题的网络爬虫配置与实现
  11. 【Web】CDN加速效果浅析
  12. oracle 日志学习(转载)
  13. BZOJ1176: [Balkan2007]Mokia CDQ分治
  14. Cacti添加threshold、monitor和setting
  15. IOS使用不同父类的 view 进行约束
  16. mybatis 基础
  17. org.apache.jasper.JasperException: /pages/column.jsp (line: 8, column: 1) File &quot;pathTags.jsp&quot; not f
  18. 团队作业7-Beta版本冲刺计划及安排
  19. 如何彻底的删除MySQL数据库(图文教程)
  20. Linux系统下apt-get命令无法安装常见软件包?

热门文章

  1. ECMAScript 2019(ES10)新特性简介
  2. 10- JMeter5.1.1 工具快速入门
  3. 软件篇-04-OMPL和FCL用于SLAM轨迹规划
  4. php异常及错误信息捕获并记录日志实现方法全解析
  5. 数据库的读写分离(Amoeba)
  6. hdu4915 判断括号匹配
  7. POJ3114强连通+spfa
  8. windows-DLL注入
  9. 【antd Vue】封装upload图片上传组件(返回Base64)
  10. 一、Github+Pycharm基础