Explicit Interface Implementation (C# Programming Guide)
2016-03-10 09:50
585 查看
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.
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:
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:
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个接口,使用同一个名称分别声明了一个属性和一个方法
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:
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 0; } int ILeft.P { get { return 0; } } }
相关文章推荐
- String,StringBuilder,StringBuffer的区别
- [LeetCode]62. Unique Paths
- Interfaces (C# Programming Guide)
- 修改UIcontrollerTableView某一行的高度
- KVC中setValuesForKeysWithDictionary:
- LeetCode Distinct Subsequences
- 背水一战 Windows 10 (2) - UI: 概述, 启动屏幕, 屏幕方向
- build st-crowd
- MathType requires a newer version of MT Extra等MathType问题的不兼容性解决方案
- UIWindow 实现遮盖导航条的蒙版
- Mysql 连接数据库超时设置autoReconnect=true
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
- jquer “$” 扩展(笔记)
- lintcode-easy-Unique Paths II
- lintcode-easy-Unique Paths
- 非UI线程可不可以更新UI(二)
- 《EasyUI》——给DataGrid动态绑定表头和数据,实现表头与数据的真正解耦
- UITextField - 输入框
- lightOJ 1082 Array Queries
- 关于封装了gevent的request grequest库的使用与讨论