java 8 接口里的默认方法特性-亚博电竞手机版

本文由码农网 – civic5216原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划!

这篇文章我们将要探讨java 8中接口里的默认方法特性。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();  } }

更深入的了解阅读,可以参考下面的链接:

  • https://jcp.org/en/jsr/detail?id=335
  • https://docs.oracle.com/javase/tutorial/java/iandi/defaultmethods.html

译文链接:http://www.codeceo.com/article/java-8-interface-default-method.html
英文原文:java 8 – default methods in interfaces
翻译作者:码农网 – civic5216
转载必须在正文中标注并保留原文链接、译文链接和译者等信息。]

展开全文
内容来源于互联网和用户投稿,文章中一旦含有亚博电竞手机版的联系方式务必识别真假,本站仅做信息展示不承担任何相关责任,如有侵权或涉及法律问题请联系亚博电竞手机版删除

最新文章

网站地图