我想在数组中填写一个方法。但我必须以一种奇怪的方式做到这一点:
public class AssignFunctionalInterfaceTest {
public void test() {
TestEntity t = new TestEntity();
// compiles
Supplier<Integer> a = t::getB;
Object[] b = new Object[] {"42", a};
// compiles
Object[] b2 = new Object[] {"42", (Supplier<Integer>) t::getB};
// doesn't compile. Error message:
// "The target type of this expression must be a functional interface"
Object[] b3 = new Object[] {"42", t::getB};
}
public static class TestEntity {
public Integer getB() {
return 0;
}
}
}
我真的想找到一种方法让第三项任务发挥作用。任何想法?
答案 0 :(得分:0)