Lambda uden fix interface og med annotation arguments
Hi,jeg har foelgende klasser:
---
public class MyFunctionManager {
public void addFunction(Object action) {
// ...
}
}
---
public class MyApp {
public void registerFunctions() {
final MyFunctionManager myFunctionManager = new MyFunctionManager();
myFunctionManager.addFunction(new Object() {
public void doSomething(
@Arg("op1") Integer op1,
@Arg("op2") Integer op2) {
System.out.println("result: " + (op1 + op2));
}
});
myFunctionManager.addFunction(new Object() {
public void doSomething(
@Arg("op1") Integer op1,
@Arg("op2") Integer op2,
@Arg("op3") Integer op3) {
System.out.println("result: " + (op1 * op2 * op3));
}
});
}
}
---
Mit problem er, at jeg altid er noedt til at lave en ny instance a Object for at vaere i stand til at wrappe min function.
Er det muligt at faa ovenstaaende eksempel til noget der ligner lambda expressions, som i foelgende eksempel?
---
public class MyApp {
public void registerFunctions() {
final MyFunctionManager myFunctionManager = new MyFunctionManager();
myFunctionManager.addFunction((
@Arg("op1") Integer op1,
@Arg("op2") Integer op2) -> {
System.out.println("result: " + (op1 + op2));
});
myFunctionManager.addFunction((
@Arg("op1") Integer op1,
@Arg("op2") Integer op2,
@Arg("op3") Integer op3) -> {
System.out.println("result: " + (op1 * op2 * op3));
});
}
}
---