Java 接口中定义的方法的“默认”实现是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18286235/
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
What is the "default" implementation of method defined in an Interface?
提问by gifpif
In the Collection Interface I found a method named removeIf()
that contains its implementation.
在集合接口中,我找到了一个名为的方法removeIf()
,其中包含它的实现。
default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
final Iterator<E> each = iterator();
while (each.hasNext()) {
if (filter.test(each.next())) {
each.remove();
removed = true;
}
}
return removed;
}
I want to know if there is any way to define method body in an interface?
What is the default
keyword and how does it work?
我想知道是否有任何方法可以在接口中定义方法体?
什么是default
关键字,它是如何工作的?
采纳答案by gifpif
From https://dzone.com/articles/interface-default-methods-java
来自https://dzone.com/articles/interface-default-methods-java
Java 8 introduces “Default Method” or (Defender methods) new feature, which allows developer to add new methods to the interfaces without breaking the existing implementation of these interface. It provides flexibility to allow interface define implementation which will use as default in the situation where a concrete class fails to provide an implementation for that method.
Java 8 引入了“默认方法”或(Defender 方法)新特性,它允许开发人员在不破坏这些接口的现有实现的情况下向接口添加新方法。它提供了允许接口定义实现的灵活性,该实现将在具体类无法为该方法提供实现的情况下用作默认值。
public interface A {
default void foo(){
System.out.println("Calling A.foo()");
}
}
public class ClassAB implements A {
}
There is one common question that people ask about default methods when they hear about the new feature for the first time:
当人们第一次听说新功能时,他们会问一个关于默认方法的常见问题:
What if the class implements two interfaces and both those interfaces define a default method with the same signature?
如果该类实现了两个接口并且这两个接口都定义了一个具有相同签名的默认方法怎么办?
Example to illustrate this situation:
举例说明这种情况:
public interface A {
default void foo(){
System.out.println("Calling A.foo()");
}
}
public interface B {
default void foo(){
System.out.println("Calling B.foo()");
}
}
public class ClassAB implements A, B {
}
This code fails to compile with the following result:
此代码无法编译,结果如下:
java: class Clazz inherits unrelated defaults for foo() from types A and B
To fix that, in Clazz, we have to resolve it manually by overriding the conflicting method:
为了解决这个问题,在 Clazz 中,我们必须通过覆盖冲突方法来手动解决它:
public class Clazz implements A, B {
public void foo(){}
}
But what if we would like to call the default implementation of method foo() from interface A instead of implementing our own.
但是如果我们想从接口 A 调用方法 foo() 的默认实现而不是实现我们自己的呢?
It is possible to refer to A#foo() as follows:
可以按如下方式引用 A#foo():
public class Clazz implements A, B {
public void foo(){
A.super.foo();
}
}
回答by Rohit Jain
Those methods are called default methods. Default methodor Defender methodis one of the newly added featuresin Java 8.
这些方法称为默认方法。默认方法或Defender 方法是Java 8 中新增的功能之一。
They will be used to allow an interface method to provide an implementation used as default in the event that a concrete class doesn't provide an implementation for that method.
它们将用于允许接口方法提供在具体类不提供该方法的实现时用作默认实现的实现。
So, if you have an interface, with a default method:
所以,如果你有一个接口,使用默认方法:
public interface Hello {
default void sayHello() {
System.out.println("Hello");
}
}
The following class is perfectly valid:
以下类是完全有效的:
public class HelloImpl implements Hello {
}
If you create an instance of HelloImpl
:
如果您创建一个实例HelloImpl
:
Hello hello = new HelloImpl();
hello.sayHello(); // This will invoke the default method in interface
Useful Links:
有用的链接:
回答by Aniket Thakur
I did a bit of research and i found the following. Hope this helps.
我做了一些研究,我发现了以下内容。希望这可以帮助。
Existing problem
存在的问题
Normal interface methods are declared as abstract and must be defined in the class that implements the interface. This 'burdens' the class implementer with the responsibility to implement every declared method. More importantly, this also means that extending an interface is not possible after 'publication'. Otherwise, all implementers would have to adapt their implementation, breaking backwards source and binary compatibility.
普通接口方法被声明为抽象方法,并且必须在实现该接口的类中定义。这使类实现者“负担”了实现每个声明方法的责任。更重要的是,这也意味着在“发布”后无法扩展接口。否则,所有实现者都必须调整他们的实现,破坏向后的源代码和二进制兼容性。
Solution adopted in Java 8
Java 8 中采用的解决方案
To cope with these problems, one of the new features of JDK 8 is the possibility to extend existing interfaces with default methods. Default methods are not only declared, but also defined in the interface.
为了解决这些问题,JDK 8 的新特性之一是可以使用默认方法扩展现有接口。默认方法不仅声明,而且在接口中定义。
Important points to note
需要注意的要点
- Implementers can choose not to implement default methods in implementing class.
- Implementers can still override default methods, like regular non-final class methods can be overridden in subclasses.
- Abstract classes can even (re)declare default methods as abstract, forcing subclasses to reimplement the method (sometimes called 're-abstraction').
- 实现者可以选择在实现类中不实现默认方法。
- 实现者仍然可以覆盖默认方法,就像可以在子类中覆盖常规的非 final 类方法一样。
- 抽象类甚至可以(重新)将默认方法声明为抽象方法,从而迫使子类重新实现该方法(有时称为“重新抽象”)。