C# explicit interface implementation(显式接口实现)
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(显式接口实现)的更多相关文章
- DbContext 中的 Explicit interface implementation
疑惑 前段时间一直再用Entity Framework 6,写了一些公用的方法,在这个过程中发现了DbContext实现的接口IObjectContextAdapter,可以通过这个接口访问到更底层的 ...
- Explicit Interface Implementation (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173157.aspx If a class implements two interfaces that con ...
- C#中的 隐式与显式接口实现
在C#中,正常情况下使用接口的实现使用的是 隐式接口实现. public interface IParent1 { void Medthod(); } public class Child : IPa ...
- iOS开发—在@interface,@implementation和@property中变量的定义
一直搞不懂在OC中变量在@interface和@implementation中有什么区别,定义@property又有什么不同,查了很多资料,总结如下: //ViewController.h @inte ...
- Guice 学习(五)多接口的实现( Many Interface Implementation)
1.接口 /* * Creation : 2015年6月30日 */ package com.guice.InterfaceManyImpl; public interface Service { p ...
- C# InterFace 接口
接口设计方式 自顶向下 (如图所示),自底向上. 接口成员: 事件 public interface IDrawingObject { event EventHandler ShapeChanged; ...
- 改善C#程序的50种方法
为什么程序已经可以正常工作了,我们还要改变它们呢?答案就是我们可以让它们变得更好.我们常常会改变所使用的工具或者语言,因为新的工具或者语言更富生产力.如果固守旧有的习惯,我们将得不到期望的结果.对于C ...
- 关于C#你应该知道的2000件事
原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...
- C# 事件Event(个人整理)
内容来源:MSN:https://docs.microsoft.com/zh-cn/dotnet/csharp/event-pattern 操作符详解(上) https://www.youtube ...
随机推荐
- Python中的子进程并发
date: 2019-06-16 22:35:33 author: headsen chen notice:个人原创 实例代码: import os,time time.sleep(1) from ...
- ASP如何将table导出EXCEL表格
网页导出excel表格非常常用,对于一些加载<table>的数据网页,经常会用到这种功能,下面和大家分享一下ASP如何导出EXCEL表格 工具/原料 ASP编辑器 方法/步骤 ...
- js实现文本框支持加减运算的方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/T ...
- c语言 正则表达式 IP地址
#include <stdio.h> #include <string.h> #include <regex.h> #define SUBSLEN 10 /* 匹配 ...
- Chrome 浏览器自动填表呈现淡黄色解决
Chrome 浏览器,当记住用户名和密码后,下次填写表单时,被记住的部分会被填充为淡黄色,有些时候不好看. 解决方式如下: input:-webkit-autofill { -webki ...
- Java 8 Steam 例子整理
Java 8 Steam 例子整理 kexue 关注 2016.06.06 17:44* 字数 1860 阅读 3901评论 0喜欢 6 IBM: Java 8 中的 Streams API 详解 为 ...
- 编译安装python3事出错:
configure: error: no acceptable C compiler found in $PATH 问题解决 解决方法: yum intall gcc -y
- ORM连表操作
连表操作分为以下三种情景: 一对多:models.ForeignKey(其他表) 多对多:models.ManyToManyField(其他表) 一对一:models.OneToOneField(其他 ...
- JS 回车提交,兼容IE、火狐、Opera、Chrome、Safari……
1.JavaScript 方法: <script> document.onkeydown=function(event){ e = event ? event ...
- 【C/C++】【VS开发】结构体存储空间数据对齐说明
关于内存对齐 一: 1.什么是内存对齐 假设我们同时声明两个变量: char a; short b; 用&(取地址符号)观察变量a, b的地址的话,我们会发现(以16位CPU为例): 如果a的 ...