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 ...
随机推荐
- 中南大学2019年ACM寒假集训前期训练题集(基础题)
先写一部分,持续到更新完. A: 寒衣调 Description 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆 ...
- CF919F A Game With Numbers
题目:(luogu翻译错的很多) Alice和Bob玩游戏,每人有8张牌,牌的值为0~4.每一轮当前玩家选择自己的牌A和对手的牌B,然后将A的值变为( A + B )%5,其中A和B都不是0. 当一个 ...
- [Python3网络爬虫开发实战] 1.8.1-pyspider的安装
pyspider是国人binux编写的强大的网络爬虫框架,它带有强大的WebUI.脚本编辑器.任务监控器.项目管理器以及结果处理器,同时支持多种数据库后端.多种消息队列,另外还支持JavaScript ...
- Format 格式化函数
转自:老百姓 Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助进行一些翻译,让它有一个完整的概貌,以供大家查询之用: 首先看它的声明:function Format(const ...
- Mysql:零散记录
limit用法 查询第4行记录 select * from tablename limit 3,1; limit 3,1:截取第3行加1行的数据 查询第6-15行 select * from tabl ...
- Python和Java的语法对比,语法简洁上python的确完美胜出
Python是一种广泛使用的解释型.高级编程.通用型编程语言,由吉多·范罗苏姆创造,第一版发布于1991年.可以视之为一种改良(加入一些其他编程语言的优点,如面向对象)的LISP.Python的设计哲 ...
- Django之CBV和FBV
Django之CBV和FBV CBV和FBV是C和F的区别: C是Class,F是Function 在请求中,有GET请求和POST请求. 在写CBV时,url是可以对应一个类的,在类中,分别写出GE ...
- Html、Css、JavaScript 遇到的问题总结
$('body').scrollTop()无效得解决方案 鼠标滑轮获取到得值为0:var scrollTop = $('body').scrollTop(); 在页面中加一个随着页面滚动条滚动的小图片 ...
- 集训第四周(高效算法设计)M题 (扫描法)
原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...
- echarts的简单应用之(二)饼图
接上一篇文章: echarts的简单应用之(一)柱形图:https://www.cnblogs.com/jylee/p/9359363.html 本篇文章讲述饼图,撇过折线图不说,是因为折线图与柱形图 ...