Java8中的函数如何获取Cls :: func(Cls cls)的引用返回Object;

时间:2018-08-01 02:05:54

标签: java function java-8

我使用以下测试用例来测试java.util.function.Function。

public class TestFunc {

  public String nonStaticFunc1 () {
    return "nonStaticAndPara of fun1";
  }
  public String nonStaticFunc1 (TestFunc tf) {
    return "nonStaticWithPara of fun1";
  }
  public String nonStaticFunc2 (TestFunc tf) {
    return "nonStaticWithPara of fun2";
  }
  @Test
  public void testFunc () {
    Function<TestFunc, String> func = TestFunc::nonStaticFunc1;
    System.out.println(func.apply(new TestFunc()));
    // Cannot make a static reference to the non-static method nonStaticFunc2(TestFunc) from the type TestFunc
    // Function<TestFunc, String> func2 = TestFunc::nonStaticFunc2;
  }
}
/* OUTPUT
 nonStaticAndPara of fun1
*///:~

始终与TestFunc :: nonStaticFunc1(void)对应的函数返回String;我检查了Function源代码:

public interface Function<T, R> {
  R apply(T t);
}

当函数的参数为​​空时,java会自动将一个类对象导入其中。那我怎样才能得到一个TestFunc :: nonStaticFunc1(TestFunc)呢?预先感谢。

1 个答案:

答案 0 :(得分:0)

这是完全错误的。

  

java自动导入类对象

它是面向对象编程语言中的函数式编程。 有必要知道您正在使用或调用哪个对象。

我不是讲师,所以让我们留待以后的解释,看看我修改后的代码:

import java.util.function.Function;

public class TestFunc {
    private final String name;

    public String getName() {
        return name;
    }

    public TestFunc(String name) {
        this.name = name;
    }

    public String nonStaticFunc1() {
        return "nonStaticAndPara of fun1 (no further parameter): " + this.name;
    }

    public String nonStaticFunc1(TestFunc parameter) {
        // parameter is received but ignored
        return "nonStaticWithPara of fun1 (parameter is received but ignored): " + this.name;
    }

    public String nonStaticFunc2(TestFunc parameter) {
        // parameter is received and used
        return "nonStaticWithPara of fun2 (parameter is received and used): " + parameter.getName();
    }

    public void testFunc(TestFunc parameter) {
        Function<TestFunc, String> func;

        func = TestFunc::nonStaticFunc1;
        System.out.println(func.apply(parameter));

        func = testFunc -> testFunc.nonStaticFunc1();
        System.out.println(func.apply(parameter));
        // TestFunc::nonStaticFunc1
        // means
        // calling nonStaticFunc1 from the parameter as in `apply(parameter)`

        func = this::nonStaticFunc1;
        System.out.println(func.apply(parameter));

        func = testFunc -> this.nonStaticFunc1(testFunc);
        System.out.println(func.apply(parameter));
        // this::nonStaticFunc1
        // means
        // this.nonStaticFunc1(testFunc)

        // similarly func2
        func = this::nonStaticFunc2;
        System.out.println(func.apply(parameter));

        func = testFunc -> this.nonStaticFunc2(testFunc);
        System.out.println(func.apply(parameter));

        // func = TestFunc::nonStaticFunc2; // doesn't make sense, there's no such thing as parameter of the parameter
    }

    public static void main(String[] args) {
        TestFunc parameter = new TestFunc("param");
        TestFunc caller = new TestFunc("caller");
        caller.testFunc(parameter);
    }
}

输出:

nonStaticAndPara of fun1 (no further parameter): param
nonStaticAndPara of fun1 (no further parameter): param
nonStaticWithPara of fun1 (parameter is received but ignored): caller
nonStaticWithPara of fun1 (parameter is received but ignored): caller
nonStaticWithPara of fun2 (parameter is received and used): param
nonStaticWithPara of fun2 (parameter is received and used): param

我在代码中留下了一些注释作为注释,以解释一些基本逻辑。希望您熟悉lambda。

编辑:减少示例代码中的对象数量 EDIT2:更改nonStaticFunc2的工作方式(现在改为使用参数)