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 ...
随机推荐
- java spark list 转为 RDD 转为 dataset 写入表中
package com.example.demo; import java.util.ArrayList; import java.util.Arrays; import java.util.Hash ...
- 移动端安卓和 IOS 开发框架 Framework7 布局
对应的各种效果,Framework7 里面实现的方式比较多,这里我就只写我用的一种,样式有的自己修改了的,想看官方详细的参见 http://framework7.cn 一.手风琴布局Accordion ...
- 流式数据处理在百度数据工厂的应用与实践 原创: 李俊卿 AI前线 今天
流式数据处理在百度数据工厂的应用与实践 原创: 李俊卿 AI前线 今天
- Benchmark result without MONITOR running: Benchmark result with MONITOR running (redis-cli monitor > /dev/null): 吞吐量 下降约1半 Redis监控工具,命令和调优
https://redis.io/commands/monitor In this particular case, running a single MONITOR client can reduc ...
- H5内嵌原生app
前言 其实我们不管是从ios还是安卓都可以看出 原生app能内嵌H5的原因是因为有了webview这个app内嵌浏览器视图,从而使得我们可以开发html然后加载到app中(原理几乎跟pc端请求.加载. ...
- 关于tomcat-users.xml添加用户被重复加载的问题mark
今天用Eclipse启动tomcat带不起来项目,想进tomcat管理页面看我的工程是否已经发布,但是管理页面没有配置tomcat用户,于是进行tomcat-user.xml的用户添加. 本来想当然地 ...
- Function mysql_db_query() is deprecated 错误解决
方法一:@ 在任何错误语句之前加上@符号,即可屏蔽! 方法二:error_reporting 在PHP文件第一行加上:error_reporting(0); 即可屏蔽! 方法三:display_err ...
- C#依赖注入实例
http://qing.weibo.com/tj/400082fa33001h7x.html 1.5 实现依赖注入1.5.1 背景介绍 设计模式中,尤其是结构型模式很多时候解决的就是对象间的依赖关系, ...
- osgViewer::Viewer::Windows
osg自带窗口去掉边框 #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #incl ...
- Vue.js学习之简介(待续)
Vue.js 渐进式JavaScript 框架 易用:已经会了 HTML.CSS.JavaScript?即刻阅读指南开始构建应用! 灵活:不断繁荣的生态系统,可以在一个库和一套完整框架之间自如伸缩. ...