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