Interfaces (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173156.aspx
An interface contains definitions for a group of related functionalities that a class or a struct can implement.
By using interfaces, you can, for example, include behavior from multiple sources in a class.
That capability is important in C# because the language doesn't support multiple inheritance of classes.
In addition, you must use an interface if you want to simulate inheritance for structs, because they can't actually inherit from another struct or class.
You define an interface by using the interface keyword, as the following example shows.
interface IEquatable<T>
{
bool Equals(T obj);
}
Any class or struct that implements the IEquatable<T> interface must contain a definition for an Equals method that matches the signature that the interface specifies.
As a result, you can count on依靠 a class that implements IEquatable<T> to contain an Equals method with which an instance of the class can determine whether it's equal to another instance of the same class.
The definition of IEquatable<T> doesn’t provide an implementation for Equals. The interface defines only the signature.
In that way, an interface in C# is similar to an abstract class in which all the methods are abstract.
However, a class or struct can implement multiple interfaces, but a class can inherit only a single class, abstract or not.
Therefore, by using interfaces, you can include behavior from multiple sources in a class.
For more information about abstract classes, see Abstract and Sealed Classes and Class Members.
Interfaces can contain methods, properties, events, indexers, or any combination of those four member types.
For links to examples, see Related Sections.
An interface can't contain constants, fields, operators, instance constructors, destructors, or types.
Interface members are automatically public, and they can't include any access modifiers.
Members also can't be static.
To implement an interface member, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member.
When a class or struct implements an interface, the class or struct must provide an implementation for all of the members that the interface defines.
The interface itself provides no functionality that a class or struct can inherit in the way that it can inherit base class functionality.
However, if a base class implements an interface, any class that's derived from the base class inherits that implementation.
The following example shows an implementation of the IEquatable<T> interface.
The implementing class, Car, must provide an implementation of theEquals method.
public class Car : IEquatable<Car>
{
public string Make {get; set;}
public string Model { get; set; }
public string Year { get; set; } // Implementation of IEquatable<T> interface
public bool Equals(Car car)
{
if (this.Make == car.Make &&
this.Model == car.Model &&
this.Year == car.Year)
{
return true;
}
else
return false;
}
}
Properties and indexers of a class can define extra accessors for a property or indexer that's defined in an interface.
For example, an interface might declare a property that has a get accessor.
The class that implements the interface can declare the same property with both a get and set accessor.
However, if the property or indexer uses explicit implementation, the accessors must match.
For more information about explicit implementation, seeExplicit Interface Implementation (C# Programming Guide) and Interface Properties (C# Programming Guide).
Interfaces can implement other interfaces.
A class might include an interface multiple times through base classes that it inherits or through interfaces that other interfaces implement.
However, the class can provide an implementation of an interface only one time and only if the class declares the interface as part of the definition of the class (class ClassName : InterfaceName).
If the interface is inherited because you inherited a base class that implements the interface, the base class provides the implementation of the members of the interface.
However, the derived class can reimplement the interface members instead of using the inherited implementation.
A base class can also implement interface members by using virtual members.
In that case, a derived class can change the interface behavior by overriding the virtual members.
For more information about virtual members, see Polymorphism.
An interface has the following properties:
An interface is like an abstract base class. Any class or struct that implements the interface must implement all its members.
An interface can't be instantiated directly. Its members are implemented by any class or struct that implements the interface.
Interfaces can contain events, indexers, methods, and properties.
Interfaces contain no implementation of methods.
A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces.
Explicit Interface Implementation (C# Programming Guide)
Explains how to create a class member that’s specific to an interface.
How to: Explicitly Implement Interface Members (C# Programming Guide)
Provides an example of how to explicitly implement members of interfaces.
How to: Explicitly Implement Members of Two Interfaces (C# Programming Guide)
Provides an example of how to explicitly implement members of interfaces with inheritance.
See Also
Interfaces (C# Programming Guide)的更多相关文章
- Generic Interfaces (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/kwtft8ak(v=vs.140).aspx It is often useful to define interf ...
- Polymorphism (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173152.aspx Polymorphism is often referred to as the thir ...
- 【IOS笔记】View Programming Guide for iOS -1
原文:View Programming Guide for iOS View and Window Architecture Views and windows present your applic ...
- View Programming Guide for iOS_读书笔记[正在更新……]
原文:View Programming Guide for iOS 1 Introduction 先熟悉一下基本概念. Window Windows do not have any visible c ...
- View Controller Programming Guide for iOS---(二)---View Controller Basics
View Controller Basics Apps running on iOS–based devices have a limited amount of screen space for d ...
- View Controller Programming Guide for iOS---(一)---About View Controllers
About View Controllers View controllers are a vital link between an app’s data and its visual appear ...
- View Programming Guide for iOS ---- iOS 视图编程指南(五)---Animations
Animations Animations provide fluid visual transitions between different states of your user inter ...
- View Programming Guide for iOS ---- iOS 视图编程指南(三)---Windows
Windows Every iOS application needs at least one window—an instance of the UIWindow class—and some m ...
- View Programming Guide for iOS ---- iOS 视图编程指南(二)---View and Window Architecture
View and Window Architecture 视图和窗口架构 Views and windows present your application’s user interface and ...
随机推荐
- 用1天快速上手org-mode(windows系统)
Table of Contents 1. 选择Emacs的理由--Org-mode 1.1. 现状(基于本人现有软件的使用) 1.2. 理念(够用才好) 1.3. 学习过程(少走弯路) 2. 快速安装 ...
- [Python3网络爬虫开发实战] 6.2-Ajax分析方法
这里还以前面的微博为例,我们知道拖动刷新的内容由Ajax加载,而且页面的URL没有变化,那么应该到哪里去查看这些Ajax请求呢? 1. 查看请求 这里还需要借助浏览器的开发者工具,下面以Chrome浏 ...
- node assert.equal()
浅测试,使用等于比较运算符(==)来比较 actual 和 expected 参数. const assert = require('assert'); assert.equal(1, 1); // ...
- HDU 4465 递推与double的精确性
题目大意不多说了 这里用dp[i][0] 代表取完第一个盒子后第二个盒子剩 i 个的概率,对应期望就是dp[i][0] *i dp[i][1] 就代表取完第二个盒子后第一个盒子剩 i 个的概率 dp[ ...
- Labeling Balls(poj 3687)
题意:N个球,从1-N编号,质量不同,范围1-N,无重复.给出小球间的质量关系(<), 要求给每个球贴标签,标签表示每个球的质量.按编号输出每个球的标签.如果解不唯一,按编号小的质量小排. /* ...
- vagrant的学习 之 ThinkPHP5.1
vagrant的学习 之 ThinkPHP5.1 本文根据慕课网的视频教程练习,感谢慕课网! 慕课视频学习地址:https://www.imooc.com/video/14218. 慕课的参考文档地址 ...
- Linux下tmp文件夹的文件自动删除的问题(转)
场景: 近日发现有一台机器tmp文件夹下放置的文件无辜丢失,而且排查发现是自动丢失,并且,只是删除10天之前的文件. 本来以为是哪位写了一个自动执行脚本, find了一下10天前的文件删除了. 结果, ...
- zabbix学习系列之QQ消息报警
安装依赖包 环境 Zabbix: 3.2 OS:Centos 安装依赖包 yum install lrzsz chrony gcc gcc-c++ git openssl-devel perl-Ext ...
- [TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking
TypeScript 2.0 introduced a new primitive type called never, the type of values that never occur. It ...
- 每天记录一点:NetCore获得配置文件 appsettings.json vue-router页面传值及接收值 详解webpack + vue + node 打造单页面(入门篇) 30分钟手把手教你学webpack实战 vue.js+webpack模块管理及组件开发
每天记录一点:NetCore获得配置文件 appsettings.json 用NetCore做项目如果用EF ORM在网上有很多的配置连接字符串,读取以及使用方法 由于很多朋友用的其他ORM如S ...