Interfaces
阅读Java的官方Doc,总结如下。
What is Interface
An interface is a reference type, similar to a class, that can contain only
- constants (implicitly public, static, final)
- method signatures (no method body, no braces)
- default methods (has method body)
- static methods (has method body)
- 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:
- Not mention the default method at all, which lets your extended interface inherit the default method.
- Redeclare the default method, which makes it abstract.
- 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();
最新文章
- 在使用 vscode 时 eslint 检测 .vue 文件中的less 部分内容
- Scala入门之控制结构
- Returns: range-based (not absolute) index within the current range
- MP3/视频播放
- WinForm点击按钮在对应的panel里画图
- WINCE6.0+ILI9806E休眠唤醒显示异常问题
- POJ 1035 Spell checker 简单字符串匹配
- IOS APP配置.plist汇总(转自coolweather )
- QT是否流行还是和历史有关啊(各个平台不同时间的方案都讲到了)
- java学习笔记day07
- ByteBuffer用法总结
- 2.3.9 用NPOI操作EXCEL--通过NPOI获得公式的返回值
- centos 6.5 安装composer
- 《Windows编程循序渐进》——MFC封装机制详解
- Ubuntu 14.04 64bit 系统下打开PHPadmin时出现缺少mysqli|mysql 扩展的解决方法(php5)
- java表达式类型的自动提升
- Android指南针之加速度传感器地磁传感器-android学习之旅(67)
- 第一个jQuery
- [20190212]删除tab$记录的恢复3.txt
- Spark-Unit2-Spark交互式命令行与SparkWordCount