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 ...
随机推荐
- webpack介绍和使用
一webpack介绍1由来2介绍3作用4拓展说明5webpack整体认知二webpack安装1安装node2安装cnpm3安装nrm的两种方法4安装webpack三webpack配置0搭建项目结构1初 ...
- javaScript(拼写树形)+ajax请求,去后台查找数据
第一步:页面加载完成时,利用jquery中的一函数,调用js方法,js方法,发送ajax请求,去后台查找父类权限集合,响应回来json格式的数据,对数据进行操作,往页面上添加内容 //页面初始化加载菜 ...
- 软件:IIS上配置CGI
本文的内容是:在Windows7中的IIS6.1中配置CGI功能. 我先讲步骤,步骤全用图来说明,以方便技术还是不熟练的朋友,以下是在Windows7系统来完成的. 目录 一.安装IIS步骤: 0X0 ...
- libcurl在mac上编译
wget http://ftp.gnu.org/gnu/m4/m4-1.4.17.tar.gz wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.t ...
- udf提权小结
00x1 首先判断mysql版本, mysql版本 < 5.2 , UDF导出到系统目录c:/windows/system32/ mysql版本 > 5.2 ,UDF导出到安装路径MySQ ...
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)--续
2.PHP与Mysqli扩展,面向过程.对象 <?php $mysql_conf = array( 'host' => '127.0.0.1:3306', 'db' => 'test ...
- osg qt fbx ifc bim osg ive 3ds max rvt
项目环境变量配置 include E:\Qt\Qt5.12.2\5.12.2\msvc2017_64\include E:\OpenSourceGraph\OpenSceneGraph_install ...
- npm的问题【解决】
1.解决npm下载慢的问题,使用该命令 npm install --registry=https://registry.npm.taobao.org 好处:比起cnpm官网解释的,这个更好,使用cnp ...
- charles 偏好设置
本文参考:charles 偏好设置 charles 偏好设置 偏好设置,注意作用如下 用户界面 视图 启动设置 警告设置 视图选项 头和主体一起查看 请求和响应页查看 结构试图布局 序列试图布局 显 ...
- Java中用FileInputStream和FileOutputStream读写txt文件,文件内容乱码的问题,另附循环代码小错误
乱码问题大概就是编码格式不一样,搜了很多都是这么说的,修改编码解决乱码问题链接: https://blog.csdn.net/weixin_42496466/article/details/81189 ...