在没有默认方法特性时,当你往接口中添加新方法时,接口内部所有实现的类都要历经一些修改,这将导致上千行的代码修改工作量。为了避免这点,Java8引入了默认对象方法,亦即,如果你想要往现存的接口中添加任何功能,你只需添加默认方法特性而不会影响接口实现形式。那么java 8 接口默认方法有哪些特性,跟yjbys小编一起来来看看吧!

让我们看一些例子来更好的理解它。例如,我声明了一个具有打开和读取功能的接口“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();

}

}

java8 接口调用默认方法_Java8接口里的默认方法特性相关推荐

  1. java接口 调用哪个_Java通过接口调用方法的时候,怎么知道调用的是哪个实现类里的方法?...

    一个抽象接口,有很多实现类,通过接口调用某个方法的时候,怎么知道调用的是哪个实现类里的方法? 谢谢. 2014-07-02 更新 前三个回答是 可以 ,不过依然没有找到我想要的答案.比如在 Eclip ...

  2. socket接口调用 java_Java中socket接口调用(示例代码)

    最近一个项目中接口通讯这一块主要是调用银联系统的socket接口,我方是客户端,即发送请求接收返回报文的一方.在贴代码之前,还是要了解一下关于socket的基础知识. Socket的基本概念 1.建立 ...

  3. Nginx代理浏览器接口调用本地的后端接口

    注意你只需要看脚本,改的都是nginx.conf文件,下面的废话可以跳过 1.找到你要代理的地址 比如我下面写到的脚本举例 最终请求到的后端地址是这个->192.168.2.167:39612/ ...

  4. 基于php的足球联赛接口调用,足球数据API接口 - 【队员资料】API调用示例代码

    野子电竞数据官网改版https://www.xxe.io/全新登场 import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshal ...

  5. Go 学习笔记(71)— Go 接口 interface (接口定义、接口实现、接口调用、值接收者、指针接收者)

    1. 接口的定义 接口是和调用方的一种约定,它是一个高度抽象的类型,不用和具体的实现细节绑定在一起.接口要做的是定义好约定,告诉调用方自己可以做什么,但不用知道它的内部实现,这和我们见到的具体的类型如 ...

  6. 发现 postman 自动生成接口调用代码的一个问题

    postman 是程序员经常使用的一个接口测试和验证工具. 我们在 postman 里点击下图按钮,能够在 Code Snippet 里,看到该接口调用对应的不同编程语言里的实现代码. 比如自动生成对 ...

  7. rtsp协议_如何在RTSP协议视频智能平台EasyNVR未登录的情况下调用通道直播的接口?...

    原标题:如何在RTSP协议视频智能平台EasyNVR未登录的情况下调用通道直播的接口? TSINGSEE青犀视频云边端架构全线都提供了丰富的API接口,用户可以自由调用进行二次开发.在本文之前,我们博 ...

  8. 授权后接口调用(UnionID)

    通过code获取access_token 接口说明 通过code获取access_token的接口. 请求说明 http请求方式: GET https://api.weixin.qq.com/sns/ ...

  9. 聊一聊声明式接口调用与Nacos的结合使用

    背景 对于公司内部的 API 接口,在引入注册中心之后,免不了会用上服务发现这个东西. 现在比较流行的接口调用方式应该是基于声明式接口的调用,它使得开发变得更加简化和快捷. .NET 在声明式接口调用 ...

最新文章

  1. system.gc会立即执行垃圾回收吗_JVM垃圾回收系列之 垃圾回收器
  2. “约见”面试官系列之常见面试题第四十篇之双向绑定以及实现原理(建议收藏)
  3. Github+jsDelivr为脚本/图片等静态文件加速的全球CDN
  4. HTML学习笔记:设置超链接文本修饰
  5. 数据库工作笔记009---Centos中导出mysql数据库
  6. vuex中mutations数据响应
  7. 利用百度搜索结果爬取邮箱
  8. 浮点错误是什么意思_Excel函数计算常见错误值,都是什么意思
  9. 火山PC大漠插件源码开源学习--木塔老师
  10. R语言数据异常值处理
  11. matlab画收敛曲线,3.26 面收敛处理
  12. 如何在AI中复制路径制作唯美动画
  13. 2020-11-19学习记录(keer‘s bug与gdb bug)
  14. 50行实现C语言FM收音机-Taskbus Stdio封装器在SDR课程中的应用
  15. 58同城一面【前端】
  16. Flex to Excel or Excel to Flex
  17. vscode 报错:Your shell has not been properly configured to use ‘conda activate‘
  18. kali安装burpsuite专业版
  19. 内存耗用:VSS/RSS/PSS/USS
  20. 一文详解8种异常检测算法(附Python代码)

热门文章

  1. 福州java培训哪里好_南京Java培训哪家好?
  2. Codeforces 1159A A pile of stones
  3. php excel 读取日期问题
  4. 智能客户端(SmartClient)
  5. 【公告】【公告】【公告】【公告】
  6. 在执行方法和Web资源中获取传递过来参数的值
  7. 面试lua笔试题各种坑
  8. HDU 1114(没有变形的完全背包)
  9. 课后作业-结对编程项目总结
  10. Django--form验证及错误处理