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 工具加一些虚拟的 ...
随机推荐
- Java语言实现简单FTP软件------>上传下载队列窗口的实现(七)
1.首先看一下队列窗口的界面 2.看一下上传队列窗口的界面 3.看一下下载队列窗口的界面 4.队列窗口的实现 package com.oyp.ftp.panel.queue; import stati ...
- http常见错误
100:继续 客户端应当继续发送请求.客户端应当继续发送请求的剩余部分,或者如果请求已经完成,忽略这个响应. 101: 转换协议 在发送完这个响应最后的空行后,服务器将会切换到在Upgrade 消 ...
- c#序列化反序列化工具(json,binary,xml)
using System; using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Bina ...
- [C/C++标准库]_[0基础]_[交集和补集]
场景: 1. 计算std::vector A和 std::vector B里的同样的元素, 用于保留不删除. 2. 计算std::vector A和 std::vector B里各自的补集, 用于删除 ...
- repo简介
高通的Android JellyBean库已经建立完成,目前已经可以正常访问,可以正常提交.Repo获取代码注意:1. 首先应有一个repo脚本,并将脚本放到环境变量里,此脚本的配置方法a) 进入用户 ...
- Android 基于Netty的消息推送方案之字符串的接收和发送(三)
在上一篇文章中<Android 基于Netty的消息推送方案之概念和工作原理(二)> ,我们介绍过一些关于Netty的概念和工作原理的内容,今天我们先来介绍一个叫做ChannelBuffe ...
- javascript 知识点坑
1. JavaScript事件属性 event.target 当目标事件发生span里面 当目标事件发生在main里面 e.target; // 目标节点DOM结构 e.target.id; // ...
- 运行第一个Hadoop程序,WordCount
系统: Ubuntu14.04 Hadoop版本: 2.7.2 参照http://www.cnblogs.com/taichu/p/5264185.html中的分享,来学习运行第一个hadoop程序. ...
- 在Linux系统中如何把文件拷贝到U盘
Linux下把所有的都当成文件处理,如果在linux系统下需要拷贝文件,哪么你需要先把U盘挂载到系统中的某一个位置,然后再使用cp命令完成拷贝. 工具/原料 Linux操作系统一台 U盘一枚 方法 ...
- (二)一个工作任务引起的乱战——C++程序编译为dll,让C#调用
C++程序编译为C#可调用的dll的过程: 1.新建一个Win32 Console Application 项目,项目名为:DLLDemo,下一步选择Application type为DLL; 2.在 ...