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

推荐订阅源

Engineering at Meta
Engineering at Meta
G
GRAHAM CLULEY
Attack and Defense Labs
Attack and Defense Labs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
T
Threat Research - Cisco Blogs
阮一峰的网络日志
阮一峰的网络日志
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
Google DeepMind News
Google DeepMind News
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
C
CXSECURITY Database RSS Feed - CXSecurity.com
量子位
T
The Blog of Author Tim Ferriss
Project Zero
Project Zero
Webroot Blog
Webroot Blog
雷峰网
雷峰网
C
Cyber Attacks, Cyber Crime and Cyber Security
Microsoft Azure Blog
Microsoft Azure Blog
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
T
The Exploit Database - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
A
About on SuperTechFans
NISL@THU
NISL@THU
AWS News Blog
AWS News Blog
Security Latest
Security Latest
S
Schneier on Security
W
WeLiveSecurity
K
Kaspersky official blog
有赞技术团队
有赞技术团队
Cyberwarzone
Cyberwarzone
P
Palo Alto Networks Blog
TaoSecurity Blog
TaoSecurity Blog
G
Google Developers Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Schneier on Security
Schneier on Security
博客园_首页
博客园 - 司徒正美
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Check Point Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Know Your Adversary
Know Your Adversary
P
Privacy & Cybersecurity Law Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"

博客园 - 修多

mac环境下无法更新OFFICE时,可以直接下载离线升级包 AxureRP_for_chorme的安装和使用方法 解决国内gem不能用的问题 ASP操作Word的权限配置 北京天天有雨下 项目记事 声声慢-李清照 荷兰风车 要继续消失了 孤独的要窒息 消失归来 新疆帕米尔高原 长白山天池 人生的几个好习惯[转载] 今天的心情本来很好 涅槃后方能重生 为心中理想奋斗! 心态决定着今天! 从郁闷到爽,真是够爽的! 昨晚睡得较早,今早精神爽啊 联想“精神病”,文摘 3.29-4.10安排 Windows Server 2003环境下安装JBuilderX 十八个故事[转载]—值得一看 属性重载的简单说明 索引指示器 static readonly与使用const的区别 操作符重载
接口实现的继承机制
修多 · 2004-03-23 · via 博客园 - 修多

一个类继承了它的基类提供的所有接口的实现。
如果不显式地重新实现接口,派生类就无法改变从基类中继承来的接口映射。
using System;

interface IControl
{
    void Paint();
}

class Control:IControl
{
    public void Paint() { Console.WriteLine("Control invoke!"); }
}

class TextBox:Control
{
    new public void Paint() { Console.WriteLine("TextBox invoke!"); }
}

class Invoke
{
    public static void Main()
    {
        Control c = new Control();
        TextBox t = new TextBox();
        IControl ic = c;
        IControl it = t;
        c.Paint();  // invokes Control.Paint();
        t.Paint();  // invokes TextBox.Paint();
        ic.Paint(); // invokes Control.Paint();
        it.Paint(); // invokes Control.Paint();

    }
}

但是,当一个interface方法被映射到类中的一个虚方法时,派生类就可以重载这个虚方法,并且改变这个接口的实现
interface IControl
{
    void Paint();
}
class Control:IControl
{
    public virtual void Paint() {...}
}
class TextBox:Control
{
    public override void Paint() {...}
}
代码效果:
Control c = new Control();
TextBox t = new TextBox();
IControl ic = c;
IControl it = t;
c.Paint();  // 调用Control.Paint();
t.Paint();  // 调用TextBox.Paint();
ic.Paint(); // 调用Control.Paint();
it.Paint(); // 调用TextBox.Paint();

  因为显式说明的接口成员不能被声明为虚的,因此无法重载显式说明的接口实现。
  这时最好采用

显式说明的接口实现来调用另一个方法,这个被调用的方法可以被声明为虚的,允许被派生类重载。
interface IControl
{
    void Paint();
}
class Control:IControl
{
    void IControl.Paint() {PaintControl();}
    protected virtual void PaintControl() {...}
}
class TextBox:Control
{
    protected override void PaintControl() {...}
}
这里,从Control中派生的类可以通过重载PaintControl方法来具体实现IControl.Paint。