Interfaces
2015-11-09 12:48
288 查看
阅读Java的官方Doc,总结如下。
constants (implicitly public, static, final)
method signatures (no method body, no braces)
default methods (has method body)
static methods (has method body)
nested types
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 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.
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.
What is Interface
An interface is a reference type, similar to a class, that can contain onlyconstants (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();
相关文章推荐
- hrabs的数据库session的修改
- 编程之美-寻找发帖“水王”方法整理
- 重新获取货运号
- 黑马程序员-----反射
- Linux 下系统调用的三种方法
- beginEdit()和endEdit()到底用来干吗的?
- Mac OS X下安装Java 7及配置Eclipse JDK
- 2015/11/9用Python写游戏,pygame入门(8):按钮和游戏结束
- CString、TCHAR*、char*转换
- HDU 1238 Substrings 细心的模拟
- python培训Day3 随笔
- 学习笔记十一:面向对象编程
- 3D Touch
- [转]STM32学习笔记6-定时器中断
- saltstack minion端状态监控程序
- lock()与lockInterruptibly()的区别
- Search for a Range
- BZOJ 1486: [HNOI2009]最小圈
- 第一写博客
- Spring MVC异常处理详解(转)