如果功能接口扩展了另一个接口,它仍然是功能接口吗?
if functional interface extends another interface is it still a functional interface?
Java 8 在 Raoul-Gabriel Urma、Mario Fusco 和 Alan Mycroft 的《行动》一书中统计:
public interface Adder{
int add(int a, int b);
}
public interface SmartAdder extends Adder{
int add(double a, double b);
}
SmartAdder isn’t a functional interface because it specifies two
abstract methods called add (one is inherited from Adder).
在本书的另一个类似示例中,以下接口称为功能接口。
public interface ActionListener extends EventListener {
void actionPerformed(ActionEvent e);
}
与第二个示例相比,第一个示例不是功能性界面的原因是什么?
因为SmartAdder
提供两个方法定义(即add
是重载,而不是覆盖):
int add(double a, double b);
,以及
int add(int a, int b);
(来自parent)
相反,EventListener
是标记接口,所以ActionListener
只提供一个方法定义,它自己的actionPerformed
。
EventListener
是一个空接口,因此扩展它的 ActionListener
只有一种方法 - public void actionPerformed(ActionEvent e)
。因此它是一个功能接口。
另一方面,SmartAdder
有两个抽象方法(int add(int a, int b)
和int add(double a, double b)
),所以它不能是函数式接口。
Java 8 在 Raoul-Gabriel Urma、Mario Fusco 和 Alan Mycroft 的《行动》一书中统计:
public interface Adder{
int add(int a, int b);
}
public interface SmartAdder extends Adder{
int add(double a, double b);
}
SmartAdder isn’t a functional interface because it specifies two abstract methods called add (one is inherited from Adder).
在本书的另一个类似示例中,以下接口称为功能接口。
public interface ActionListener extends EventListener {
void actionPerformed(ActionEvent e);
}
与第二个示例相比,第一个示例不是功能性界面的原因是什么?
因为SmartAdder
提供两个方法定义(即add
是重载,而不是覆盖):
int add(double a, double b);
,以及int add(int a, int b);
(来自parent)
相反,EventListener
是标记接口,所以ActionListener
只提供一个方法定义,它自己的actionPerformed
。
EventListener
是一个空接口,因此扩展它的 ActionListener
只有一种方法 - public void actionPerformed(ActionEvent e)
。因此它是一个功能接口。
另一方面,SmartAdder
有两个抽象方法(int add(int a, int b)
和int add(double a, double b)
),所以它不能是函数式接口。