这篇文章我们将要探讨Java8中接口里的默认方法特性。Java8指出“默认方法使得新功能被添加到库中的接口里面,同时又能保证与这些接口老版本代码的二进制兼容性。”
这些年Java进化升级了很多,在Java库中引入的接口需要添加新的功能。在没有默认方法特性时,当你往接口中添加新方法时,接口内部所有实现的类都要历经一些修改。这将导致上千行的代码修改工作量。为了避免这点,Java 8引入了默认对象方法。亦即,如果你想要往现存的接口中添加任何功能,你只需添加默认方法特性而不会影响接口实现形式。
让我们看一些例子来更好的理解它。例如,我声明了一个具有打开和读取功能的接口“BookIterface”。接口的类需要实现打开和读取方法。
package org.smarttechie;
/**
- The interface is intended to open and read. The implementors should implement the methods to open and read.
- @author Siva Prasad Rao Janapati
/
public interface BookInterface {
/*
- The method opens the book
/
public void openTheBook();
/* - The method reads the book
*/
public void readTheBook();
}现在,我们提供上面接口的实现代码。
package org.smarttechie;
/**
- The JavaBookImpl is the implementation of BookInterface
- @author Siva Prasad Rao Janapati
/
public class JavaBookImpl implements BookInterface {
/*
- This opens the book
*/
@Override
public void openTheBook() {
System.out.println(“The Java book is opened”);
}
/**
- This reads the book
*/
@Override
public void readTheBook() {
System.out.println(“Reading the Java book”);
}
}现在,我们想要给接口提供一个关闭功能。如果你直接添加关闭功能到book接口中,现存的实现类需要历经一些修改。有了默认方法特性后,我们能给book接口直接添加关闭功能。默认方法对所有实现都可用。
package org.smarttechie;
/**
- The interface is intended to open and read. The implementors should implement the methods to open and read.
- @author Siva Prasad Rao Janapati
/
public interface BookInterface {
/*
- The method opens the book
/
public void openTheBook();
/* - The method reads the book
/
public void readTheBook();
/* - The default method implementation
/
public default void closeTheBook() {
System.out.println(“Closting the book”);
}
}package org.smarttechie;
/* - The JavaBookImpl is the implementation of BookInterface
- @author Siva Prasad Rao Janapati
/
public class JavaBookImpl implements BookInterface {
/*
- This opens the book
/
@Override
public void openTheBook() {
System.out.println(“The Java book is opened”);
}
/* - This reads the book
*/
@Override
public void readTheBook() {
System.out.println(“Reading the Java book”);
}
public static void main (String[] args) {
BookInterface bookInter = new JavaBookImpl();
//Call the default method declared in BookInterface
bookInter.closeTheBook();
JavaBookImpl book = new JavaBookImpl();
book.closeTheBook();
}
}下面给出了上述调用方法的字节码。从字节码中,我们可以认为默认方法是一种“虚方法”。
如果你想,你可以重载实现类中的默认方法:
package org.smarttechie;
/**
- The JavaBookImpl is the implementation of BookInterface
- @author Siva Prasad Rao Janapati
/
public class JavaBookImpl implements BookInterface {
/*
- This opens the book
/
@Override
public void openTheBook() {
System.out.println(“The Java book is opened”);
}
/* - This reads the book
/
@Override
public void readTheBook() {
System.out.println(“Reading the Java book”);
}
/ - This closes the book
*/
public void closeTheBook() {
System.out.println(“Closing the JAVA book”);
}
public static void main (String[] args) {
BookInterface book = new JavaBookImpl();
book.closeTheBook();
}
}到这会儿,你可能有一个疑问,如果我们实现了两个具有同样默认方法签名的接口会怎样?在那种情况下,调用实现会得到下面的编译错误提示。
“用参数()和()复制名为closedTheBook的默认方法是继承于TheBookInterface和BookInterface的类型。”
package org.smarttechie;
public interface TechBookInterface {
/**
- The default method implementation
/
public default void closeTheBook() {
System.out.println(“Closing the book”);
}
}package org.smarttechie;
/* - The JavaBookImpl is the implementation of BookInterface
- @author Siva Prasad Rao Janapati
/
public class JavaBookImpl implements BookInterface, TechBookInterface {
/*
- This opens the book
/
@Override
public void openTheBook() {
System.out.println(“The Java book is opened”);
}
/* - This reads the book
*/
@Override
public void readTheBook() {
System.out.println(“Reading the Java book”);
}
public static void main (String[] args) {
BookInterface book = new JavaBookImpl();
book.closeTheBook();
}
}为了避免这个编译错误,我们需要在实现类中显式地定义那个具有同样签名的方法。
package org.smarttechie;
/**
- The JavaBookImpl is the implementation of BookInterface
- @author Siva Prasad Rao Janapati
/
public class JavaBookImpl implements BookInterface, TechBookInterface {
/*
- This opens the book
/
@Override
public void openTheBook() {
System.out.println(“The Java book is opened”);
}
/* - This reads the book
*/
@Override
public void readTheBook() {
System.out.println(“Reading the Java book”);
}
public void closeTheBook() {
System.out.println(“Closing the JAVA book”);
}
public static void main (String[] args) {
BookInterface book = new JavaBookImpl();
book.closeTheBook();
}
}
东莞网站建设www.zg886.cn