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 设计模式之访问者模式
写在前面 设计模式是经过总结.优化的,对我们经常会碰到的一些编程问题的可重用解决方案.一个设计模式并不像一个类或一个库那样能够直接作用于我们的代码.反之,设计模式更为高级,它是一种必须在特定情形下实现 ...
- Matrix: Matrix的,postScale(), postTranslate()方法
Matrix的操作,总共分为translate(平移),rotate(旋转),scale(缩放)和skew(倾斜)四种,每一种变换在Android的API里都提供了set, post和pre三种操作方 ...
- Git<一> 手工编辑冲突
一:背景 Neo君之前在写东西时,都是自己负责各自的模块,没有出现代码拉下来冲突的情况.最近Neo君在搞一搞前端的东东,跟同事功能有些冲突,所以就难免会冲突. 所以简单总结下,这次针对的情况是不同的用 ...
- Centos7.4服务器安装Laravel5.7详细讲解(2018-10-27)
一.在阿里云或者腾讯云选择Centos7并购买服务器 二.安装宝塔面板和php运行环境 1.输入命令 yum install -y wget && wget -O install.sh ...
- Faster RCNN论文学习
Faster R-CNN在Fast R-CNN的基础上的改进就是不再使用选择性搜索方法来提取框,效率慢,而是使用RPN网络来取代选择性搜索方法,不仅提高了速度,精确度也更高了 Faster R-CNN ...
- ifc tree
ViewerWidget* viewerWidget = new ViewerWidget(ifcModel); viewerWidget ->setRootNode(ifcModel-> ...
- WCAG
WCAG What is WCAG? Web Content Accessibility Guidelines (WCAG) Overview Checklist and solve technolo ...
- mysql正则替换某个字段值里面的某个字符串
sql语句如下: UPDATE `ccvms_video` SET title=REPLACE(title, "最", "相对") WHERE title LI ...
- Jenkins备份插件backup
这个插件可以备份Jenkins的JENKINS_HOME目录,并恢复,但是版本太旧了. 有一些其他插件可以定时备份,搜索Backup https://www.cnblogs.com/cxwblog/p ...
- 异地协作,A地上传jar包到B地服务器上传速率慢
在A地使用ftp服务器,再登录B地的目标服务器,使用ftp命令从ftp服务器下载文件,速度快点,下载带宽比上传带宽要大一点 https://blog.csdn.net/df0128/article/d ...