java @FunctionalInterfaces 可以有默认方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30165060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can @FunctionalInterfaces have default methods?
提问by membersound
Why can't I create a @FunctionalInterface
with a default method implementation?
为什么我不能@FunctionalInterface
用默认方法实现创建一个?
@FunctionalInterface
public interface MyInterface {
default boolean authorize(String value) {
return true;
}
}
回答by Alexis C.
You can have default methods in a functional interfacebut its contract requires you to provide one single abstract method (or SAM). Since a default method have an implementation, it's not abstract.
您可以在功能接口中使用默认方法,但其约定要求您提供一个抽象方法(或 SAM)。由于默认方法具有实现,因此它不是抽象的。
Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract.
从概念上讲,函数式接口只有一个抽象方法。由于默认方法具有实现,因此它们不是抽象的。
and
和
If a type is annotated with this annotation type, compilers are required to generate an error message unless:
The type is an interface type and not an annotation type, enum, or class.
The annotated type satisfies the requirements of a functional interface.
如果使用此注释类型对类型进行注释,则编译器需要生成错误消息,除非:
该类型是接口类型,而不是注释类型、枚举或类。
带注释的类型满足功能接口的要求。
Here you don't satisfy the functional interface's requirement, so you need to provide one abstract method. For example:
这里不满足函数式接口的要求,需要提供一个抽象方法。例如:
@FunctionalInterface
interface MyInterface {
boolean authorize(int val);
default boolean authorize(String value) {
return true;
}
}
Note that if you declare an abstract method overriding one of a public method from the Object's class it doesn't count, because any implementation of this interface will have an implementation of those methods through at least the Object's class. For example:
请注意,如果您声明了一个抽象方法来覆盖 Object 类中的一个公共方法,则它不会计算在内,因为该接口的任何实现都将至少通过 Object 类实现这些方法。例如:
@FunctionalInterface
interface MyInterface {
default boolean authorize(String value) {
return true;
}
boolean equals(Object o);
}
does not compile.
不编译。
回答by Holger
A functional interfaceis an interface
having a single abstractmethod. The entire purpose of defining functional interfaces is to enable the implementation of the single abstract method via lambda expressions which will effectively override that method which makes providing a default
implementation for it pointless.
甲功能接口是一个interface
具有单一的抽象方法。定义函数式接口的全部目的是通过 lambda 表达式实现单个抽象方法,这将有效地覆盖该方法,这使得为其提供default
实现毫无意义。
Having an interface
consisting entirely of default
methods raises multiple problems. There is the technical problem that the compiler can't decide for a lambda expression which method to implement when there are multiple default
methods and there is the semantic problem that an interface
consisting entirely of default
methods is not abstract
. You can't instantiate this default behavior as you can't instantiate interface
s and are forcing programmers to create concrete classes just to invoke the default behavior, which, since interfaces are stateless, could be provided by a singleton instead:
拥有一个interface
完全由default
方法提出了多个问题。存在技术问题,即当有多个default
方法时,编译器无法为 lambda 表达式决定要实现哪个方法,并且存在interface
完全由default
方法组成的语义问题is not abstract
。你不能实例化这个默认行为,因为你不能实例化interface
s 并且迫使程序员创建具体的类来调用默认行为,因为接口是无状态的,可以由单例提供:
@FunctionalInterface
public interface MyInterface {
static MyInterface DEFAULT = s->true;
boolean authorize(String value);
}
Note that you can have interfaces extending a functional interface and providing a default method, if you need. Still, if this results in creating an interface
having no abstract methods I would question the design. You may compare with the discussion about marker interfaces with default
methods. If the sub-interface will have different abstract methods than the functional interface, it's a different story. There might be real use cases for this, but these sub-interfaces will also demonstrate why they shouldn't be mixed with the functional base interface
as a lambda expression will always implement the abstractmethod.
请注意,如果需要,您可以让接口扩展功能接口并提供默认方法。尽管如此,如果这导致创建一个interface
没有抽象方法的我会质疑设计。您可以与有关带default
方法的标记接口的讨论进行比较。如果子接口将具有与功能接口不同的抽象方法,那就是另一回事了。这可能有实际用例,但这些子接口也将说明为什么它们不应该与函数基础混合,interface
因为 lambda 表达式将始终实现抽象方法。
回答by Tagir Valeev
That's because @FunctionalInterface
can have default methods, as many as you want. For example, consider the java.util.Function
interface. It contains two default methods: compose
and andThen
. But there should be exactly one non-default method. Otherwise how compiler would know which of your default methods should be mapped to lambda?
那是因为@FunctionalInterface
可以有默认方法,只要你想。例如,考虑java.util.Function
接口。它包含两个默认方法:compose
和andThen
。但是应该只有一种非默认方法。否则编译器怎么知道你的哪个默认方法应该映射到 lambda?
回答by Amandeep Rohila
I just want to add a few more points.
我只想补充几点。
We can have any number of Abstractmethod in FuntionalInterface.
We can also have any number of Staticmethod in FuntionalInterface.
We can also declare an abstract method overriding one of a public method from the Object's class but there must be some other custom abstract method in this functional interface too as shown in below code
@FunctionalInterface public interface SAM { public void helloSam();
default void xyz() { System.out.println("xyz"); } static void abc() { System.out.println("abc"); } static void abc1() { System.out.println("abc1"); } default void xyz1() { System.out.println("xyz1"); } boolean equals(Object o); }
我们可以在 FuntionalInterface 中拥有任意数量的Abstract方法。
我们还可以在 FuntionalInterface 中拥有任意数量的静态方法。
我们也可以声明一个抽象方法来覆盖 Object 类中的一个公共方法,但是在这个功能接口中也必须有一些其他的自定义抽象方法,如下面的代码所示
@FunctionalInterface 公共接口 SAM { public void helloSam();
default void xyz() { System.out.println("xyz"); } static void abc() { System.out.println("abc"); } static void abc1() { System.out.println("abc1"); } default void xyz1() { System.out.println("xyz1"); } boolean equals(Object o); }