惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
H
Help Net Security
The Cloudflare Blog
Y
Y Combinator Blog
A
Arctic Wolf
Cyberwarzone
Cyberwarzone
G
Google Developers Blog
Recent Announcements
Recent Announcements
S
SegmentFault 最新的问题
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
博客园 - Franky
罗磊的独立博客
Martin Fowler
Martin Fowler
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 三生石上(FineUI控件)
N
News and Events Feed by Topic
F
Fortinet All Blogs
N
News | PayPal Newsroom
J
Java Code Geeks
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
博客园 - 聂微东
S
Securelist
C
CERT Recently Published Vulnerability Notes
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
S
Security Affairs
NISL@THU
NISL@THU
A
About on SuperTechFans
PCI Perspectives
PCI Perspectives
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Jina AI
Jina AI
Microsoft Azure Blog
Microsoft Azure Blog
AWS News Blog
AWS News Blog
GbyAI
GbyAI
C
Cyber Attacks, Cyber Crime and Cyber Security
V
Vulnerabilities – Threatpost
D
Docker
P
Proofpoint News Feed
W
WeLiveSecurity
Help Net Security
Help Net Security
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
The Hacker News
The Hacker News
博客园 - 叶小钗

博客园 - 张远强

原版SQLHelper.cs下载 Chrome浏览器Proxy Switchy、Tampermonkey等扩展程序下载 GreaseMonkey脚本:阻止Google转换搜索链接地址 站在31岁,理解程序员年过三十这道坎 GreaseMonkey让网站登录验证码形同虚设 DateTime.ToString格式限定符转义 借Windows说明Linux分区和挂载点 JSON解析类(C#) 彻底解决ASP.NET MD5加密中文结果和ASP不一致的问题 SharpZipLib使用示例 DotNetZip使用示例 Dvbbs 8.2.0 RC1多功能编辑器,提供下载 B/S系统版本管理V1.0正式发布 ASP.NET 2.0缓存 个人网站与动网整合步骤(支持PDO1.0) - 张远强 - 博客园 C#接口范例 巧用escape解决ASP.NET中URL传参乱码问题 使用Filter实现信息的二次检索 使用overflow代替left截取指定长度字符串
C#中显/隐式实现接口及其访问方法
张远强 · 2007-12-27 · via 博客园 - 张远强

1.隐式实现的接口

 1interface IControl
 2{
 3    void Paint();
 4}

 5public class EditBox : IControl
 6{
 7    public void Paint()
 8    {
 9        Console.WriteLine("Pain method is called!");
10    }

11}

12class Test
13{
14    static void Main()
15    {
16        EditBox editbox = new EditBox();
17        editbox.Paint();
18        ((IControl)editbox).Paint();
19        Console.ReadKey();
20    }

21}

结果:

Pain method is called!
Pain method is called!

说明:从实例中我们可以看到用隐式实现的接口既可以通过类来访问,也可以通过接口来访问!

2.显式实现的接口

 1interface IControl
 2{
 3    void Paint();
 4}

 5public class EditBox : IControl
 6{
 7    void IControl.Paint()
 8    {
 9        Console.WriteLine("IControl.Pain method is called!");
10    }

11}

12class Test
13{
14    static void Main()
15    {
16        EditBox editbox = new EditBox();
17        //editbox.Paint();//通过类访问会出错
18        ((IControl)editbox).Paint();
19        Console.ReadKey();
20    }

21}

结果:

1IControl.Pain method is called!

说明:从实例中我们可以看到用显式实现的接口只能通过接口来访问,如果试图通过类来访问会出错:““ConsoleApplication1.EditBox”并不包含“Paint”的定义。”

3.同时用显/隐式实现接口会怎么样?

 1interface IControl
 2{
 3    void Paint();
 4}

 5public class EditBox : IControl
 6{
 7    void IControl.Paint()
 8    {
 9        Console.WriteLine("IControl.Pain method is called!");
10    }

11    public void Paint()
12    {
13        Console.WriteLine("Pain method is called!");
14    }

15}

16class Test
17{
18    static void Main()
19    {
20        EditBox editbox = new EditBox();
21        editbox.Paint();
22        ((IControl)editbox).Paint();
23        Console.ReadKey();
24    }

25}

结果:

Pain method is called!
IControl.Pain method is called!

说明:当同时用显/隐式实现接口时,显式才是真正的接口实现方法!

4.结论

在多数情况下,我们都是用隐式来实现接口,此时既可以通过类来访问,又可以通过接口来访问,而通过显式实现的接口则只能通过接口来访问,总结一下就是:当显式实现方式存在时,隐式实现方式就失效了。但这不能表示显式实现方式就不好,当一个类实现的多个接口中具有相同的方法时,用显式方式来专门实现某个接口的方法时就显得非常有用!

5.参考文章

[1] C#中的接口:http://zhenyulu.cnblogs.com/archive/2006/04/18/377705.html