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 设计模式之 (Chain of Responsibility)责任链模式
#写在前面 对于每一种设计模式,如果不理解它的原理和结构,是写不出例子来的.所以弄明白很重要. 等过完这段浑浑噩噩的日子,我要找个遍地开花的地方开怀大笑一场 #责任链模式定义 简书上一网友就把这个定义 ...
- python小白之矩阵matrix笔记(updating)
Matrix #python学习之矩阵matrix 2018.4.18 # -*- coding: UTF-8 -*- from numpy import * import numpy as np i ...
- VisualStudio版本号
VisualStudio的工程文件,后面的数字对应的VS的版本号, 71表示的VS2003, 80表示VS2005, 90表示VS2008, 10表示VS2010等.
- Qt kdChart 甘特图案例
KDChart 甘特图在Qt中的加载使用案例,代码来自官方 mainwindow.h /******************************************************* ...
- ES6深入浅出-1 新版变量声明:let 和 const-3.视频 相关面试题
执行顺序问题 请问console.log输出的值是多少 输出的肯定是1 假如这里有一行未知的代码 会打印出几? 如果这段未知的代码是a=2.那么其实console输出的就是2 只关心代码,没有关心代码 ...
- 123467123456#1#-----com.twoapp.DaDiShuGame01--前拼后广--现实打地鼠游戏jiemei
com.twoapp.DaDiShuGame01--前拼后广--现实打地鼠游戏jiemei
- 深入理解Java虚拟机 - 书评
谈起<深入理解java虚拟机>这本书,让我印象深刻的就是换工作跳槽面试的时候,当时刚进入java开发这个行业的时候,平时只是做一些对数据库的增删改查等功能,当自己技术增长一些的时候,就开始 ...
- ElasticSearch——数据建模最佳实践
如何建模 mapping 设计非常重要,需要从两个维度进行考虑: 功能:搜索.排序.聚合 性能:存储的开锁.内存的开销.搜索的性能 mapping 注意事项: 加入新字段很容易(必要时需要 updat ...
- iOS-DatePicket
组件_DatePicket /** 1.初始化 2.设置选择控件的格式 3.设置日期选择控件的地区 4.监听日期选择控件数值变化 **/ UIDatePicker *datePicker = [[UI ...
- 生成对抗网络GAN详解与代码
1.GAN的基本原理其实非常简单,这里以生成图片为例进行说明.假设我们有两个网络,G(Generator)和D(Discriminator).正如它的名字所暗示的那样,它们的功能分别是: G是一个生成 ...