阅读Java的官方Doc,总结如下。

What is Interface

An interface is a reference type, similar to a class, that can contain only

  1. constants (implicitly public, static, final)
  2. method signatures (no method body, no braces)
  3. default methods (has method body)
  4. static methods (has method body)
  5. nested types

Keypoints

Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

A class can implement multi interfaces.

An interface can extend multi interface.

All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.

Default Methods

A pretty clear application scenario here.

Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.

When you extend an interface that contains a default method, you can do the following:

  1. Not mention the default method at all, which lets your extended interface inherit the default method.
  2. Redeclare the default method, which makes it abstract.
  3. Redefine the default method, which overrides it.

Special Case

Look at following codes. ContentVisitor is an interface.

private final ContentVisitor visitor = new ContentVisitor() {
public void onStartDocument() {
throw new IllegalStateException();
}

Did we create an instance of interface?

No. We can't instantiate an interface. Actually, what these codes do is to define an anonymous class that implements the interface, and instantiate that class.

It's the same with following codes.

private final class AnonymousContentVisitor implements ContentVisitor {
public void onStartDocument() {
throw new IllegalStateException();
}
} private final ContentVisitor visitor = new AnonymousContentVisitor();

最新文章

  1. 在使用 vscode 时 eslint 检测 .vue 文件中的less 部分内容
  2. Scala入门之控制结构
  3. Returns: range-based (not absolute) index within the current range
  4. MP3/视频播放
  5. WinForm点击按钮在对应的panel里画图
  6. WINCE6.0+ILI9806E休眠唤醒显示异常问题
  7. POJ 1035 Spell checker 简单字符串匹配
  8. IOS APP配置.plist汇总(转自coolweather )
  9. QT是否流行还是和历史有关啊(各个平台不同时间的方案都讲到了)
  10. java学习笔记day07
  11. ByteBuffer用法总结
  12. 2.3.9 用NPOI操作EXCEL--通过NPOI获得公式的返回值
  13. centos 6.5 安装composer
  14. 《Windows编程循序渐进》——MFC封装机制详解
  15. Ubuntu 14.04 64bit 系统下打开PHPadmin时出现缺少mysqli|mysql 扩展的解决方法(php5)
  16. java表达式类型的自动提升
  17. Android指南针之加速度传感器地磁传感器-android学习之旅(67)
  18. 第一个jQuery
  19. [20190212]删除tab$记录的恢复3.txt
  20. Spark-Unit2-Spark交互式命令行与SparkWordCount

热门文章

  1. 计算app内部缓存文件大小
  2. Android中设置文本颜色的三种方法
  3. 为什么HikariCP被号称为性能最好的Java数据库连接池,怎样配置使用
  4. (原创)android4.4沉浸式标题栏
  5. 从客户端检测到危险的Request.Form值解决方案
  6. Android(通用机能)
  7. C#多线程实践——线程同步
  8. C#小写人民币转大写
  9. C/C++中虚函数的调用
  10. 很强的PHP图片处理类