Java 8 - 函数式接口与抽象类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19950013/
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
Java 8 - Functional Interface vs Abstract class
提问by Batty
I was exploring features of Java 8 and came across "Functional Interface".
我在探索 Java 8 的特性时遇到了"Functional Interface"。
As per my understanding, these interfaces can have some default implemented methods as :
根据我的理解,这些接口可以有一些默认实现的方法:
@FunctionalInterface
public interface ComplexFunctionalInterface extends SimpleFuncInterface
{
default public void doSomeWork()
{
System.out.println("Doing some work in interface impl...");
}
default public void doSomeOtherWork()
{
System.out.println("Doing some other work in interface impl...");
}
}
But my doubt is, this what abstract class is for.
但我的疑问是,这就是抽象类的用途。
Why introduce functional interfaces.
为什么要引入功能接口。
采纳答案by yamafontes
But my doubt is, this what abstract class is for.
Why introduce functional interfaces.
但我的疑问是,这就是抽象类的用途。
为什么要引入函数式接口。
Number of classes that can be extended: 1
可扩展的类数: 1
Number of interfaces that can be implemented: more than 1
可以实现的接口数量: more than 1
回答by DeltaLima
I have no experience with JAVA 8 yet, but from what I can see this would allow for a kind of multiple inheritance, which is not possible with abstract classes
我还没有使用 JAVA 8 的经验,但从我所见,这将允许一种多重继承,而抽象类是不可能的
回答by sachin10
The interface Comparator is functional although it explicitly declares two methods, because only one is abstract; equals is an explicit declaration of a concrete method inherited from Object that, without this declaration, would otherwise be implicitly declared.
Comparator 接口是函数式的,虽然它显式声明了两个方法,因为只有一个是抽象的;equals 是从 Object 继承的具体方法的显式声明,如果没有这个声明,否则将被隐式声明。
回答by Evgeniy Dorofeev
Functional interfaces must have only one method. The only exception is mehtods declared in Object. See http://java.dzone.com/articles/introduction-functional-1
函数式接口必须只有一种方法。唯一的例外是在 Object 中声明的方法。见http://java.dzone.com/articles/introduction-functional-1
回答by Andrey Chaschev
Functional interface are is used in a "safe" multiple inheritance. Differences:
函数式接口用于“安全”多重继承。区别:
- A class may extend multiple functional interfaces.
- Functional interfaces may have only a single abstract method.
- Functional interfaces may not have fieldsunlike C++ abstract classes.
- 一个类可以扩展多个功能接口。
- 函数式接口可能只有一个抽象方法。
- 与 C++ 抽象类不同,函数式接口可能没有字段。
Typical usageis when you want to embed default functionality into objects. I.e. if you have a function-like object,
典型用法是当您想将默认功能嵌入到对象中时。即如果你有一个类似函数的对象,
class MyFunction1 {
public Integer apply(String s){
...
}
}
class MyFunction2 {
public List<String> apply(Integer s){
...
}
}
And you want to make a composition out of them, you just drop in implements Function
:
你想用它们组成一个组合,你只需加入implements Function
:
class MyFunction1 implements Function<String, Integer>{
public Integer apply(String s){
...
}
}
class MyFunction2 implements Function<Integer, List<String>>{
public List<String> apply(Integer s){
...
}
}
And you may create a composition of your functions. Two approaches compared:
并且您可以创建您的函数的组合。两种方法比较:
No functional interfaces:
无功能接口:
MyFunction1 myFunction1 = ...;
MyFunction2 myFunction2 = ...;
Function<String, List<String>> composition = (s) -> myFunction2.apply(myFunction1.apply(s));
With functional interfaces:
具有功能接口:
MyFunction1 myFunction1 = ...;
MyFunction2 myFunction2 = ...;
Function<String, List<String>> composition = myFunction1.andThen(myFunction2);
The difference
区别
- No need to re-implementfunctions.
- Other functions availablein the extending class:
compose
andidentity
. - New default function is made a part of a class hierarchyand there is no need to create a new object. Usually functions like
compose()
are not included into a class definition as it would result into class size growth. They are often put into separate utility classes. In Guava composition is put into a separate utility classFunctions
: Functions.compose. So with new functional interfaces you would not need to recall in which utility class your function is implemented.
- 无需重新实现功能。
- 扩展类中可用的其他函数:
compose
和identity
. - 新的默认功能是由类层次结构的一部分,而且是没有必要创建新的对象。通常像这样
compose()
的函数不包含在类定义中,因为它会导致类大小增长。它们通常被放入单独的实用程序类中。在 Guava 中,组合被放入一个单独的实用程序类Functions
:Functions.compose。因此,使用新的函数式接口,您无需回忆您的函数是在哪个实用程序类中实现的。