如何在Java8中为用户定义的接口和方法传递方法_引用中的参数

如何在Java8中为用户定义的接口和方法传递方法_引用中的参数,java,java-8,method-reference,functional-interface,Java,Java 8,Method Reference,Functional Interface,我的代码是这样的 @FunctionalInterface interface MathOperation2 { int operation2(int a, int b, int c); } public class Method_reference_demo { private static int operate2(int a, int b, int c, MathOperation2 obj) { return obj.operation2(

我的代码是这样的

@FunctionalInterface
interface MathOperation2 {
    int operation2(int a, int b, int c);
 }

public class Method_reference_demo {
    private static int operate2(int a, int b, int c, MathOperation2 obj) 
    { 
        return obj.operation2(a, b, c);
    }
    private void Method_reference_demo01() 
    {       
    MathOperation2 mo2 = Method_reference_demo::operate2;
    mo2.operation2(2,3,4);
    }
}
不管怎样,我可以通过最后两行传递参数使其工作。 意思是线下。

MathOperation2 mo2=方法\参考\演示::操作2;
mo2.操作2(2,3,4);
我想要上面的代码片段作为工作代码。
注意:除了这两行代码外,我无法更改任何代码行,我希望使用Java 8方法引用。

底线:-您需要在某个时间点提供接口的实现以利用它


代码行

MathOperation2 mo2 = Method_reference_demo::operate2
在无效声明中,由于
运算符2
方法的签名

int operate2(int a, int b, int c, MathOperation2 obj)
还需要将
MathOperation2
传递给该方法

请注意,如果您更改签名以删除最后一个参数,那么它可能会起作用,但这会使其效率低下,因为最好定义遵循相同签名的接口本身的抽象方法


除了这两行代码外,我不能更改任何代码行

然后,您可以将接口定义为:

MathOperation2 mo2 = (a, b, c) -> {
    return 0; // perform the operation with a, b and c here
};
System.out.println(mo2.operation2(2, 3, 4)); // just to print the output
例如,要添加三个整数,表示形式为:

MathOperation2 mo2 = (a, b, c) -> a + b + c;
System.out.println(mo2.operation2(2, 3, 4)); // would print '9'

(来自评论)就方法引用而言,该示例将转化为以下内容:

private static int operate2(int a, int b, int c) { // <<-- notice the method signature
    return a + b + c; // <<--  and a definition
}

private void Method_reference_demo01() {
    MathOperation2 mo2 = Method_reference_demo::operate2;
    System.out.println(mo2.operation2(2, 3, 4)); // prints '9'
}

private static int operate2(int a,int b,int c){/方法引用,
MathOperation2 mo2
应该指向该方法的一个实现,
int operation2(int a,int b,int c)

@FunctionalInterface
interface MathOperation2 {
    int operation2(int a, int b, int c);
}

public class Main {
    private static int operate2(int a, int b, int c) {
        return a + b + c;
    }

    private static void methodReferenceDemo01() {
        MathOperation2 mo2 = Main::operate2;
        System.out.println(mo2.operation2(2, 3, 4));
    }

    public static void main(String[] args) {
        methodReferenceDemo01();
    }
}
但它并没有像您提到的那样100%满足您的要求:

除了这两行代码外,我不能更改任何代码行


在旁注中,您应该遵循方法名称,例如,
method\u reference\u demo01
不遵循约定;它可以命名为
methodReferenceDemo01
,它没有任何意义。没有实际实现
MathOperation2
的对象。什么是
mo2.operation2(2,3,4)
应该做什么?你需要解释为什么你不能修改方法,为什么你想使用方法引用。是的!这没有意义。我只是想确定一下。在接口和方法修改受到限制的情况下,我们可以用方法引用替换lamda吗。是的!使用lambda是可能的。我只想使用方法引用nce而不是lambda。我们可以这样做吗?@JackDawson您需要定义一个与您的接口类似的方法(请参阅注释中提到的部分)然后可以用作方法参考。为什么删除
operate2
的最后一个参数会使它效率低下?@Holger哦,也许我描述得不对,我不是说性能效率低下,但既然OP已经有了
int operation2的方法(int a,int b,int c)
,因此从
int operate2(int a,int b,int c,MathOperation2 obj)
中删除最后一个参数只会导致定义另一个方法,该方法的签名与接口中的方法的签名类似。对于我所假设的,定义lambda可能只是一个不需要的实用程序。(我想是复习的时候了,你过一会儿就回来了。:)好吧,静态方法
operation2
是完全没有意义的,只要它所做的一切,就是在传入的
MathOperation2
对象上调用相同的方法,但是不清楚删除该参数时该方法会变成什么。唯一合理的更改是您在答案末尾发布的内容,删除参数并更改代码以执行实际操作。