Explicit Interface Implementation (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173157.aspx
If a class implements two interfaces that contain a member with the same signature,
then implementing that member on the class will cause both interfaces to use that member as their implementation.
In the following example, all the calls to Paint invoke the same method.
class Test
{
static void Main()
{
SampleClass sc = new SampleClass();
IControl ctrl = (IControl)sc;
ISurface srfc = (ISurface)sc; // The following lines all call the same method.
sc.Paint();
ctrl.Paint();
srfc.Paint();
}
} interface IControl
{
void Paint();
}
interface ISurface
{
void Paint();
}
class SampleClass : IControl, ISurface
{
// Both ISurface.Paint and IControl.Paint call this method.
public void Paint()
{
Console.WriteLine("Paint method in SampleClass");
}
}
If the two interface members do not perform the same function, however, this can lead to an incorrect implementation of one or both of the interfaces.
It is possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interface.
This is accomplished by naming the class member with the name of the interface and a period.
For example:
public class SampleClass : IControl, ISurface
{
void IControl.Paint()
{
System.Console.WriteLine("IControl.Paint");
}
void ISurface.Paint()
{
System.Console.WriteLine("ISurface.Paint");
}
}
The class member IControl.Paint is only available through the IControl interface, and ISurface.Paint is only available through ISurface.
Both method implementations are separate, and neither is available directly on the class.
For example:
// Call the Paint methods from Main. SampleClass obj = new SampleClass();
//obj.Paint(); // Compiler error. IControl c = (IControl)obj;
c.Paint(); // Calls IControl.Paint on SampleClass. ISurface s = (ISurface)obj;
s.Paint(); // Calls ISurface.Paint on SampleClass. // Output:
// IControl.Paint
// ISurface.Paint
Explicit implementation is also used to resolve cases
where two interfaces each declare different members of the same name such as a property and a method: //2个接口,使用同一个名称分别声明了一个属性和一个方法
interface ILeft
{
int P { get;}
}
interface IRight
{
int P();
}
To implement both interfaces, a class has to use explicit implementation either for the property P, or the method P, or both, to avoid a compiler error.
For example:
class Middle : ILeft, IRight
{
public int P() { return ; }
int ILeft.P { get { return ; } }
}
https://www.codeproject.com/Articles/1000374/Explicit-Interface-VS-Implicit-Interface-in-Csharp
Explicit Interface Implementation (C# Programming Guide)的更多相关文章
- DbContext 中的 Explicit interface implementation
疑惑 前段时间一直再用Entity Framework 6,写了一些公用的方法,在这个过程中发现了DbContext实现的接口IObjectContextAdapter,可以通过这个接口访问到更底层的 ...
- C# explicit interface implementation(显式接口实现)
C# explicit interface implementation 某个类要实现两个包含相同方法名的接口, 应该如何实现这两个方法? namespace ExplicitInterfaceImp ...
- Interfaces (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173156.aspx An interface contains definitions for a group ...
- 【IOS笔记】View Programming Guide for iOS -1
原文:View Programming Guide for iOS View and Window Architecture Views and windows present your applic ...
- [IoLanguage]Io Programming Guide[转]
Io Programming Guide Introduction Perspective Getting Started Downloading Installing Binaries Ru ...
- View Programming Guide for iOS_读书笔记[正在更新……]
原文:View Programming Guide for iOS 1 Introduction 先熟悉一下基本概念. Window Windows do not have any visible c ...
- View Programming Guide for iOS ---- iOS 视图编程指南(二)---View and Window Architecture
View and Window Architecture 视图和窗口架构 Views and windows present your application’s user interface and ...
- Collection View Programming Guide for iOS---(七)---Custom Layouts: A Worked Example
Custom Layouts: A Worked Example Creating a custom collection view layout is simple with straightfor ...
- View Controller Programming Guide for iOS---(七)---Resizing the View Controller’s Views
Resizing the View Controller’s Views A view controller owns its own view and manages the view’s cont ...
随机推荐
- C++ 标准模板库介绍(STL)
1. STL 基本介绍 C++ STL(标准模板库)是惠普实验室开发的一系列软件的统称,是一套功能强大的 C++ 模板类.STL的目的是为了标准化组件,这样就不用重新开发,让后来者可以使用现成的组件, ...
- MySQL 快速入门教程
转:MySQL快速 入门教程 目录 一.MySQL的相关概念介绍 二.Windows下MySQL的配置 配置步骤 MySQL服务的启动.停止与卸载 三.MySQL脚本的基本组成 四.MySQL中的数据 ...
- prop 和 attr 中一些羞羞的事情
引言 前几天做一个迷你京东小项目的时候涉及到一个全选的小功能,一开始用的是 attr,但是效果完全不是自己想要的,当商品按钮点击过一次后,attr就无法对其状态进行更改,最后谷歌了一番发现需要用 pr ...
- Redis 压缩存储的配置
如题,redis是采用了ziplist 元素在不足一定数量时采用压缩存储 hash: zset: list: 如上图所示: ziplist-entries:最大元素数量(即存储了多少个元素) zipl ...
- Python中的列表(2)
一.从列表中删除元素 使用del 语句删除. books = ['Pride and Prejudice','Jane Eyre','The Catcher in the Rye'] print(bo ...
- JavaEE JDBC 事务
JDBC 事务 @author ixenos 事务 1.概念:我们将一组语句构建成一个事务(trans action),当所有语句顺利执行之后,事务可以被提交(commit):否则,如果其中某个语句遇 ...
- 【转】Selenium专题—JQuery选择器
juqery选择器是jquery库中非常重要的功能.jquery选择器是基于CSS1-3选择器,加上一些额外的选择器.这些选择器和CSS选择器的使用方法很相似,允许开发人员简单快速的识别页面上的元素. ...
- 【BZOJ2818】Gcd(莫比乌斯反演,欧拉函数)
题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对 1<=N<=10^7 思路:莫比乌斯反演,同BZOJ2820…… ; ..max]of ...
- 【BZOJ3697】采药人的路径(点分治)
题意:采药人的药田是一个树状结构,每条路径上都种植着同种药材.采药人以自己对药材独到的见解,对每种药材进行了分类.大致分为两类,一种是阴性的,一种是阳性的.采药人每天都要进行采药活动.他选择的路径是很 ...
- Spring3.2+mybatis3.2+Struts2.3整合配置文件大全
0.配置文件目录 1.Spring配置 applicationContext-dao.xml <?xml version="1.0" encoding="UTF-8 ...