big*_*zer 8 java lambda function-pointers function-binding
下面讨论如何使用lambdas将方法作为参数传递:
Java Pass Method as Parameter
在其他语言中,即C++,可以使用Lambdas将函数绑定到它的参数 - 这里讨论:
Bind Vs Lambda?
在Java中,是否可以使用lambdas绑定方法?
如果是这样,你会如何做到这一点?
编辑>>>>
根据要求,我通常尝试做的一个例子:
请注意,这里有伪代码.
public class DataView {
private static ArrayList<Float> rectData = new ArrayList<Float>();
private static ArrayList<Float> textData = new ArrayList<Float>();
DataView(){
//Pseudo Code:
boundFunction mybind = boundFunction(functionA, 5, 10);
boundFunction mybind2 = boundFunction(functionB, 10, 12);
iter(mybind);
iter(mybind2);
}
//Method with pseudo parameter
private void iter(functionSignature){
for(Float i : textData){
//Pseudo call to pseudo parameter
functionSignature();
}
}
private void functionA(int a, int b){
//dostuff
}
private void functionB(int a, int b){
//do other stuff
}
Run Code Online (Sandbox Code Playgroud)
}
请记住,我不是在寻找'另一种实现此功能的方法' - 这个例子是为了说明我想将函数用作参数的一般方法,并将参数绑定到这些函数.
编辑>>>
尝试使用匿名类:
public class DataView {
private class Bound{
public void run(){}
}
private static ArrayList<Float> rectData = new ArrayList<Float>();
private static ArrayList<Float> textData = new ArrayList<Float>();
DataView(){
Bound mybind = new Bound(){
public void run(){
functionA(5,10);
}
};
Bound mybind2 = new Bound(){
public void run(){
functionB(5,10);
}
};
iter(mybind);
iter(mybind2);
}
private void iter(Bound function){
for(Float i : textData){
function.run();
}
}
private void functionA(int a, int b){
//dostuff
}
private void functionB(int a, int b){
//do other stuff
}
}
Run Code Online (Sandbox Code Playgroud)
如在其他答案中所述,Java需要实际的功能接口类型来表示功能.这也适用于绑定操作,甚至需要两个这样的接口,一个用于表示未绑定的函数,另一个用于绑定函数:
public class DataView {
interface NoArgFunction {
void func();
}
interface TwoIntFunction {
void func(int a, int b);
}
static NoArgFunction bind(TwoIntFunction f, int first, int second) {
return () -> f.func(first, second);
}
private static ArrayList<Float> rectData = new ArrayList<Float>();
private static ArrayList<Float> textData = new ArrayList<Float>();
DataView(){
NoArgFunction mybind = bind(this::functionA, 5, 10);
NoArgFunction mybind2 = bind(this::functionB, 10, 12);
iter(mybind);
iter(mybind2);
}
private void iter(NoArgFunction noArg){
for(Float i : textData){
noArg.func();
}
}
private void functionA(int a, int b){
//dostuff
}
private void functionB(int a, int b){
//do other stuff
}
}
Run Code Online (Sandbox Code Playgroud)
作为一个有趣的事实,在字节码级别上,有一种方法可以将方法与参数组合在一起,而不需要中间步骤,这正是lambda表达式所使用的内容() -> f.func(first, second),它被编译成一个包含lambda体并具有lambda体的合成方法.两个参数for first和second将在运行时用于构造NoArgFunction绑定当前值first和的实例second.
但是你不能在Java源代码中使用它.现有方法唯一受支持的绑定是在方法引用中看到的绑定,this::functionA并且this::functionB都将当前this实例绑定到方法functionA和functionB.好吧,
NoArgFunction mybind = () -> functionA(5, 10);在没有bind方法的情况下首先使用lambda表达式会缩短进程...
因此,最重要的是,试图与试图建模非本质支持的特征的语言进行斗争没有多大意义.使用lambda表达式而不是绑定,以及为此目的预定义的目标类型使代码更加简单:
public class DataView {
private static ArrayList<Float> rectData = new ArrayList<Float>();
private static ArrayList<Float> textData = new ArrayList<Float>();
DataView(){
iter(x -> functionA(5, 10));
iter(x -> functionB(10, 12));
}
private void iter(Consumer<Float> function){
textData.forEach(function);
}
private void functionA(int a, int b){
//dostuff
}
private void functionB(int a, int b){
//do other stuff
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5927 次 |
| 最近记录: |