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();
Interfaces的更多相关文章
- 代码的坏味道(9)——异曲同工的类(Alternative Classes with Different Interfaces)
坏味道--异曲同工的类(Alternative Classes with Different Interfaces) 特征 两个类中有着不同的函数,却在做着同一件事. 问题原因 这种情况往往是因为:创 ...
- 【转】[fix] Wireshark error: There are no interfaces on which a capture can be done. on Mac OS X
I got the following error message when trying to open a network interface for capture using Wireshar ...
- CentOS / Redhat : Configure CentOS as a Software Router with two interfaces
CentOS / Redhat : Configure CentOS as a Software Router with two interfaces Linux can be easily co ...
- 错误:java.util.Map is an interface, and JAXB can't handle interfaces.
问题: 在整合spring+cxf时报错java.util.Map is an interface, and JAXB can't handle interfaces. 解决方法: 将服务端的serv ...
- Wireshark设置interface 时提示“There are no interfaces on which a capture can be done ”
Wireshark设置interface 时提示“There are no interfaces on which a capture can be done ” 解决方法: 今天在电脑上安装了WIR ...
- iis 使用 LocalDB 报错:provider: SQL Network Interfaces, error: 50
在使用asp.net core读取localdb数据库时,报以下错误: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 S ...
- System.Data.SqlClient.SqlException: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - 定位指定的服务器/实例时出错)
A network-related or instance-specific error occurred while establishing a connection to SQL Server. ...
- Go Data Structures: Interfaces
refer:http://research.swtch.com/interfaces Go Data Structures: Interfaces Posted on Tuesday, Decembe ...
- TypeScript - Interfaces
简介 关注于数据值的 ‘shape’的类型检查是TypeScript核心设计原则.这种模式有时被称为‘鸭子类型’或者‘结构子类型化’. . 在TypeScript中接口interfaces的责任就是命 ...
- ubuntu /etc/network/interfaces 中配置虚拟链路
ubuntu /etc/network/interfaces 中配置虚拟链路 平常做一些关于网络的测试时,像一些需要在二层上运行的功能,一个网卡不足够的情况下,可使用 ip link 工具加一些虚拟的 ...
随机推荐
- 简单javaEE思维导图
- Nginx各个配置块功能详解
Nginx学习笔记-入门篇 nginx初探 ginx服务器是轻量级web服务器中广受好评的一款产品,常用功能有HTTP代理与反向代理(目前已支持七层与四层代理),负载均衡,web缓存. nginx配置 ...
- Linux命令之exit
本文链接:http://codingstandards.iteye.com/blog/836625 (转载请注明出处) 用途说明 exit命令用于退出当前shell,在shell脚本中可以终止当前 ...
- [转]Android-网络通信框架Volley使用详解
1 Volley发送get请求: public void getJson() { String url = "http://"+host+":8080/web/json. ...
- android Settings 解析
1.Settings的主界面的实现: Settings采用了PreferenceActivity和PreferenceFragment结合的实现方式. Settings.java继承自Preferen ...
- Win32函数Sleep的精度测试
用了三种方法,第一种使用高精度性能计数器:第二种是使用多媒体定时器,另一种是<Windows图形编程>里提供的CPU周期来获取.推荐第一种方式测量: 先看第一种: #include < ...
- django: db - many to one
models 模块中的对象有三种对应关系:多对一,多对多,一对一.本讲说明多对一关系. blog/models.py: from django.db import models class Emplo ...
- box-shadow全面解析
一.box-shadow语法: box-shadow: none | inset(可选值,不设置,为外投影,设置,为内投影) x-offset(阴影水平偏移量,正方向为right) y-offset( ...
- 初探CSS
css基本框架 index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- HTML5框架、背景和实体、XHTML的使用规范
HTML5框架 cols :竖排 rows:横排,frameset frame已弃用 iframe仍保留使用.用法: Target属性: HTML5实体 XHTML的使用规范