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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
A
About on SuperTechFans
量子位
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
T
Threat Research - Cisco Blogs
Spread Privacy
Spread Privacy
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News and Events Feed by Topic
C
Cybersecurity and Infrastructure Security Agency CISA
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Tenable Blog
V
V2EX
月光博客
月光博客
L
Lohrmann on Cybersecurity
W
WeLiveSecurity
Webroot Blog
Webroot Blog
H
Hacker News: Front Page
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Exploit Database - CXSecurity.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 三生石上(FineUI控件)
T
Troy Hunt's Blog
Google Online Security Blog
Google Online Security Blog
AI
AI
腾讯CDC
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V2EX - 技术
V2EX - 技术
Martin Fowler
Martin Fowler
博客园 - Franky
I
Intezer
Project Zero
Project Zero
I
InfoQ
P
Privacy International News Feed
C
Check Point Blog
T
The Blog of Author Tim Ferriss
P
Palo Alto Networks Blog
L
LINUX DO - 最新话题
有赞技术团队
有赞技术团队
Cloudbric
Cloudbric
人人都是产品经理
人人都是产品经理
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
SegmentFault 最新的问题
Latest news
Latest news
小众软件
小众软件

博客园 - 修多

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。