Say, for the following example:
public class MyConsumer {
public void accept(int i) {}
public static void biAccept(MyConsumer mc, int i) {}
}
public class BiConsumerDemo {
public void accept(int i) { }
public static void biAccept(MyConsumer mc, int i) { }
private void testBiConsume() {
BiConsumer<MyConsumer, Integer> accumulator = (x, y) -> {}; // no problem, method accepts 2 parameters
accumulator = MyConsumer::accept; // accepts only one parameter and yet is treated as BiConsumer
accumulator = MyConsumer::biAccept; // needed to be static
accumulator = BiConsumerDemo::accept; // compilation error, only accepts single parameter
}
}
Why is that the variable accumulator
, which is a BiConsumer
that requires a function to accept 2 parameters, can be assigned with MyConsumer::accept
when that method only accepts only a single parameter?
What is the principle behind this language design in Java? If there's a term for it, what is it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…