如何在没有 .Method() 的情况下调用函数式接口中的方法

mik*_*ike 5 java

public class Main {
    public static void main(String[] args) {
        MyFunctionalInterface_1 F;
        MyFunctionalInterface_2 G;
        F = X::Method;
        int i = F.Method(5);
        System.out.println(i);
        G = new X()::Method;
        G.Method();
    }
}

class X{
    public static int Method(double x){
        return (int)(x*x*x);
    }

    public void Method(){
        System.out.println("Huy sosi");
    }
}

@FunctionalInterface
interface MyFunctionalInterface_1{
    int Method(double x);
}

@FunctionalInterface
interface MyFunctionalInterface_2{
    void Method();
}
Run Code Online (Sandbox Code Playgroud)

我如何将函数称为F()G()

我在做考试题(俄语翻译):

在 Java 中编写实体声明 F、G 和 X,以便以下代码片段编译无错误

"F = X::Method; int i = F(0.0); G = new X()::Method; G();"
Run Code Online (Sandbox Code Playgroud)

我不知道我怎么能这样称呼他们。() 和 (0.0) 或者也许我应该使用不同的东西,而不仅仅是功能接口。我不是Java专家。你能帮助我吗?

And*_*ner 5

利用名称含义的规则,这是可能的:

static int F(double d) {
  return 0;
}

static void G() {}

static class X {
  void method();
}

public static void main(String[] args) {
  Consumer<X> F;
  Runnable G;

  F = X::Method; int i = F(0.0); G = new X()::Method; G();
}
Run Code Online (Sandbox Code Playgroud)

请注意,F(0.0) 它没有调用使用者,G() 也没有运行 runnable。但是,这确实符合“编译无错误”的标准。


经过一番思考,我想出了一种定义它的方法,以便方法调用与函数方法一样:

class X {
  static int F(double x) {
    return 0;
  }

  static int Method(double x) {
    return F(x);
  }

  static void G() {}

  void Method() {
    G();
  }

  public static void main(String[] args) {
    DoubleToIntFunction F;
    Runnable G;

    F = X::Method; int i = F(0.0); G = new X()::Method; G();
  }
}
Run Code Online (Sandbox Code Playgroud)

真是一团糟。