考虑以下代码:
class Test {
void accept(Consumer<Integer> c) {}
static void consumer(Integer i) {}
void foo() {
accept(this::consumer); // The method accept(Consumer<Integer>) in the type Test is not applicable for the arguments (this::consumer)
accept(Test::consumer); // Valid
}
}
前几天我无意中以非静态方式调用静态方法时遇到了这个问题。我知道您不应该以非静态方式调用静态方法,但我仍然想知道,为什么在这种情况下无法推断类型?