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 ...
随机推荐
- Chrome插件:微信公众号自动登录(chrome.extension)
manifest.json: { "manifest_version": 2, "name": "WX.AutoLogin", " ...
- Hadoop2.x异常总结
问题1: 在执行bin/hdfs namenode -format格式化HDFS命令时,抛出异常,异常如下: 16/10/26 18:32:45 ERROR namenode.NameNode: Fa ...
- UVa 712 S-Trees(二进制转换 二叉树叶子)
题意: 给定一颗n层的二叉树的叶子, 然后给定每层走的方向, 0代表左边, 1代表右边, 求到达的是那一个叶子. 每层有一个编号, 然后n层的编号是打乱的, 但是给的顺序是从1到n. 分析: 先用一个 ...
- Java异常使用要点记录
近期有离职的想法,奈何简历过于寒碜,技术懂的少,基础也薄弱,想要提升自己却不知从哪里入手.在郁闷一段时间后偶然间看到一篇博客,博主说出了许多人的心声,同时也指出了切入点,的确基础才是重点,是时候沉迷学 ...
- HDU1811 并查集+拓扑排序
题目大意: 判断是否能根据给定的规则将这一串数字准确排序出来 我们用小的数指向大的数 对于相等的情况下,将二者合并到同一个并查集中,最后抽象出来的图上面的每一个点都应该代表并查集的标号 #includ ...
- codevs1128 导弹拦截
题目描述 Description 经过11 年的韬光养晦,某国研发出了一种新的导弹拦截系统,凡是与它的距离不超过其工作半径的导弹都能够被它成功拦截.当工作半径为0 时,则能够拦截与它位置恰好相同的导弹 ...
- Linux网络设置
==========================网络设置========================== 1.IP地址 临时:ifconfig 192.168.124.129 永久: vi / ...
- Count Color POJ - 2777 线段树
Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds ...
- codevs——1385 挤牛奶
1385 挤牛奶 USACO 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题解 查看运行结果 题目描述 Description 三个农民每天清 ...
- Mutual Training for Wannafly Union #9
A(SPOJ NPC2016A) 题意:给一个正方形和内部一个点,要求从这个点向四边反射形成的路线的长度 分析:不断做对称,最后等价于求两个点之间的距离 B(CF480E) 题意:求01矩阵内由0组成 ...