Function interface in Java 8
•
Java
It's hard for me to classify why lambda expressions can be assigned to some functional interfaces instead of others For an example, use some functional interfaces from the metrics Library:
Gauge<Double> foo = () -> { return null; }; RatioGauge bar = () -> { return null; };
The second statement has a compilation error (in eclipse):
As far as I know, ratiogauge is a functional interface Did I miss anything?
Solution
An abstract class (even if it has only one abstract method) is not a functional interface Only one interface can be one
From JLS 9.8:
The original idea was to let the abstact class be expressed as lambda; They are called "Sam types" and represent "single abstract methods" This is a difficult problem to solve This thread talk about why; Basically, the constructor of the base class makes it difficult
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码