Functional Programming With Java 简明教程
Functional Programming - Default Methods
Java 8 在接口中引入了默认方法实现的新概念。添加此功能是为了向后兼容,以便可以使用旧接口来利用 Java 8 的 lambda 表达式功能。
Java 8 introduces a new concept of default method implementation in interfaces. This capability is added for backward compatibility so that old interfaces can be used to leverage the lambda expression capability of Java 8.
例如,“List”或“Collection”接口没有“forEach”方法声明。因此,添加这种方法只会破坏集合框架实现。Java 8 引入了默认方法,以便 List/Collection 接口可以具有 forEach 方法的默认实现,而实现这些接口的类无需实现相同的接口。
For example, 'List' or 'Collection' interfaces do not have 'forEach' method declaration. Thus, adding such method will simply break the collection framework implementations. Java 8 introduces default method so that List/Collection interface can have a default implementation of forEach method, and the class implementing these interfaces need not implement the same.
Multiple Defaults
使用接口中的默认函数,有可能某个类实现了具有相同默认方法的两个接口。以下代码解释了如何解决此歧义。
With default functions in interfaces, there is a possibility that a class is implementing two interfaces with same default methods. The following code explains how this ambiguity can be resolved.
public interface vehicle {
default void print() {
System.out.println("I am a vehicle!");
}
}
public interface fourWheeler {
default void print() {
System.out.println("I am a four wheeler!");
}
}
第一个解决方案是创建一个自己的方法来覆盖默认实现。
First solution is to create an own method that overrides the default implementation.
public class car implements vehicle, fourWheeler {
public void print() {
System.out.println("I am a four wheeler car vehicle!");
}
}
第二个解决方案是使用 super 来调用指定接口的默认方法。
Second solution is to call the default method of the specified interface using super.
public class car implements vehicle, fourWheeler {
default void print() {
vehicle.super.print();
}
}
Static Default Methods
从 Java 8 开始,接口还可以具有静态帮助器方法。
An interface can also have static helper methods from Java 8 onwards.
public interface vehicle {
default void print() {
System.out.println("I am a vehicle!");
}
static void blowHorn() {
System.out.println("Blowing horn!!!");
}
}
Default Method Example
在任意编辑器中创建以下 Java 程序,例如,在 C:\> JAVA 中创建。
Create the following Java program using any editor of your choice in, say, C:\> JAVA.
Java8Tester.java
public class Java8Tester {
public static void main(String args[]) {
Vehicle vehicle = new Car();
vehicle.print();
}
}
interface Vehicle {
default void print() {
System.out.println("I am a vehicle!");
}
static void blowHorn() {
System.out.println("Blowing horn!!!");
}
}
interface FourWheeler {
default void print() {
System.out.println("I am a four wheeler!");
}
}
class Car implements Vehicle, FourWheeler {
public void print() {
Vehicle.super.print();
FourWheeler.super.print();
Vehicle.blowHorn();
System.out.println("I am a car!");
}
}
Verify the Result
按照如下方式使用 javac 编译器编译类 −
Compile the class using javac compiler as follows −
C:\JAVA>javac Java8Tester.java
现在按照如下方式运行 Java8Tester −
Now run the Java8Tester as follows −
C:\JAVA>java Java8Tester
它应该产生以下输出 −
It should produce the following output −
I am a vehicle!
I am a four wheeler!
Blowing horn!!!
I am a car!