C# explicit interface implementation

某个类要实现两个包含相同方法名的接口, 应该如何实现这两个方法?

 namespace ExplicitInterfaceImplementation
{
class Program : IPrintOne,IPrintTwo, IPrintThree
{
static void Main(string[] args)
{
Program p = new Program();
p.Print();
(p as IPrintTwo).Print();
((IPrintThree)p).Print();
} public void Print()
{
Console.WriteLine("Print One Interface");
}
// explicitly implement IPrintTwo
void IPrintTwo.Print()
{
Console.WriteLine("Print Two Interface");
}
// explicitly implement IPrintThree
string IPrintThree.Print()
{
Console.WriteLine("Print two Interface");
return "asd";
}
} interface IPrintOne
{
void Print();
} interface IPrintTwo
{
void Print();
} interface IPrintThree
{
string Print();
}
}

以上Demo中共有3个拥有相同方法名的interface,Program类继承了这三个接口,使用explicit interface implementation实现IPrintTwo和IPrintThree接口的方法

显示实现的接口方法前不能加access modifier,且调用该方法时需将Program类转换位接口才能调用, 不能直接通过类引用调用。

When a class explicitly implements an interface member, the interface member can no longer be accessed thru class reference variable, but only thru the interface reference variable.

以上Demo的运行结果:

Print One Interface  
Print Two Interface  
Print Three Interface  

C# explicit interface implementation(显式接口实现)的更多相关文章

  1. DbContext 中的 Explicit interface implementation

    疑惑 前段时间一直再用Entity Framework 6,写了一些公用的方法,在这个过程中发现了DbContext实现的接口IObjectContextAdapter,可以通过这个接口访问到更底层的 ...

  2. Explicit Interface Implementation (C# Programming Guide)

    https://msdn.microsoft.com/en-us/library/ms173157.aspx If a class implements two interfaces that con ...

  3. C#中的 隐式与显式接口实现

    在C#中,正常情况下使用接口的实现使用的是 隐式接口实现. public interface IParent1 { void Medthod(); } public class Child : IPa ...

  4. iOS开发—在@interface,@implementation和@property中变量的定义

    一直搞不懂在OC中变量在@interface和@implementation中有什么区别,定义@property又有什么不同,查了很多资料,总结如下: //ViewController.h @inte ...

  5. Guice 学习(五)多接口的实现( Many Interface Implementation)

    1.接口 /* * Creation : 2015年6月30日 */ package com.guice.InterfaceManyImpl; public interface Service { p ...

  6. C# InterFace 接口

    接口设计方式 自顶向下 (如图所示),自底向上. 接口成员: 事件 public interface IDrawingObject { event EventHandler ShapeChanged; ...

  7. 改善C#程序的50种方法

    为什么程序已经可以正常工作了,我们还要改变它们呢?答案就是我们可以让它们变得更好.我们常常会改变所使用的工具或者语言,因为新的工具或者语言更富生产力.如果固守旧有的习惯,我们将得不到期望的结果.对于C ...

  8. 关于C#你应该知道的2000件事

    原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...

  9. C# 事件Event(个人整理)

    内容来源:MSN:https://docs.microsoft.com/zh-cn/dotnet/csharp/event-pattern 操作符详解(上)   https://www.youtube ...

随机推荐

  1. GCC编译流程及常用编辑命令

    GCC 编译器在编译一个C语言程序时需要经过以下 4 步: 将C语言源程序预处理,生成.i文件. 预处理后的.i文件编译成为汇编语言,生成.s文件. 将汇编语言文件经过汇编,生成目标文件.o文件. 将 ...

  2. eclipse手动添加本地jar包到本地maven仓库

    在使用maven进行构建项目时,有时候中央仓库不包含所需的jar包,就需要下载到本地后手动添加到本地仓库中.这里介绍下利用eclipse进行本地jar安装到maven本地仓库. 在Eclipse项目中 ...

  3. 自定义控件的属性declare-styleable

    在res/values文件下定义一个attrs.xml文件,代码如下: <?xml version="1.0" encoding="utf-8"?> ...

  4. MySQL5.1中文文档学习笔记——第1章:一般信息(一)

    原文地址 MySQL软件是一种开放源码软件. 开放源码"意味着任何人都能使用和改变软件.任何人都能从Internet下载MySQL软件,而无需支付任何费用.如果愿意,你可以研究源码并进行恰当 ...

  5. 【Leetcode_easy】705. Design HashSet

    problem 705. Design HashSet 题意: solution1: class MyHashSet { public: /** Initialize your data struct ...

  6. [c++]C++关键字之friend

    re 1. C++关键字之friend; end 希望大家能把自己的所学和他人一起分享,不要去鄙视别人索取时的贪婪,因为最应该被鄙视的是不肯分享时的吝啬.---GOOD---

  7. idea的enable auto-import/内存设置

    设置pom.xml依赖的自动导入 设置idea的memory heap On the Help menu, click Edit Custom VM Options. Set the -Xmx opt ...

  8. WebGL半透明物体的绘制

    WebGL 中当透明和半透明物体共存时,相关设置不正确的话,物体表面会出现破碎杂乱的断面,非常影响效果,我们接着就来解决这个问题. 完成的展示Demo请看: 半透明物体和透明物体共存 α 混合 让物体 ...

  9. 当你登录Github要求你邮箱验证身份,但是你的邮箱登录不了?

    事情发送在两天前,我如标题所示......,它给出的tyningling@163我真的不知道什么时候注册的了,尝试了N个密码登录不上,验证密保吧,看到手机号突然想起来,这是拿以前同学的手机号注册的.. ...

  10. 移动架构-AOP面向切面编程

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点, ...