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

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
V
V2EX
PCI Perspectives
PCI Perspectives
Latest news
Latest news
博客园 - 三生石上(FineUI控件)
C
CERT Recently Published Vulnerability Notes
W
WeLiveSecurity
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Palo Alto Networks Blog
T
The Exploit Database - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
V
Vulnerabilities – Threatpost
H
Heimdal Security Blog
Attack and Defense Labs
Attack and Defense Labs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - 叶小钗
V
Visual Studio Blog
Jina AI
Jina AI
P
Proofpoint News Feed
罗磊的独立博客
SecWiki News
SecWiki News
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 热门话题
Security Archives - TechRepublic
Security Archives - TechRepublic
The Hacker News
The Hacker News
Hugging Face - Blog
Hugging Face - Blog
N
News and Events Feed by Topic
NISL@THU
NISL@THU
T
Tailwind CSS Blog
T
Tenable Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recent Announcements
Recent Announcements
H
Hacker News: Front Page
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tor Project blog
宝玉的分享
宝玉的分享
Help Net Security
Help Net Security
S
Security Affairs
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
G
GRAHAM CLULEY

博客园 - pot

在框架中(IFRAME/FRAMESET)传递COOKIE的解决方案[转] jQuery图片播放插件Fancybox使用方法 C#生成缩略图代码 数据抓取的一个类,包含一些常用的方法 抓取AJAX网页的方法-Firefox组件,C#集成 C#编号的ActiveX控件采用CAB的布署方式实例 C#编写ActiveX控件实例(包括命令和事件) PdfAcroViewer C#代码登录活动目录 用XML反序列化快速完成ASP.NET配置文件 - pot - 博客园 android Activity类的使用 Android中的Intent详细讲解 Android模拟器常用使用,和基本功能使用 ZPL II 命令参考 正则表达式语法 - pot - 博客园 《C#异常处理》 The RSA key container could not be opened - pot ASP.NET 页面事件执行顺序 关于object sender,EventArgs e 的一些解释
C#中接口的作用
pot · 2008-03-24 · via 博客园 - pot

假设我们公司有两种程序员:VB程序员,指的是用VB写程序的程序员,用clsVBProgramer这个类表示;Delphi程序员指的是用 Delphi写程序的程序员,用clsDelphiProgramer这个类来表示。 每个类都有一个WriteCode()方法。定义如下:

class clsVBProgramer()
{
  ....
  WriteCode()
  
{
     
//用VB语言写代码;
  }

  ....
}


class clsDelphiProgramer()
{
  ....
  WriteCode()
  
{
    
//用Delphi语言写代码;
  }

   ....
}


现在公司来了一个项目,要求派某个程序员写一个程序。
class clsProject()
{
  ....
  WritePrograme(clsVBProgramer programer)
//用VB写代码
  {
    programer.WriteCode();
  }

  WritePrograme(clsDelphiProgramer programer)
//重载方法,用Delphi写代码
  {
    programer.WriteCode();
  }

 ......
}

在主程序中我们可以这样写:
main()
{
   clsProject proj
=new  clsProject;
   
//如果需要用VB写代码
   clsVBProgramer programer1=new clsVBProgramer;
   proj.WritePrograme(programer1);
   
//如果需要用Delphi写代码
   clsDelphiProgramer programer2=new clsDelphiProgramer;
   proj.WritePrograme(programer2);
}


但是如果这时公司又来了一个C#程序员,我们怎么改这段程序,使它能够实现用C#写程序的功能呢?我们需要增加一个新类 clsCSharpProgramer,同时在此clsProject这个类中要再次重载WritePrograme (clsCSharpProgramer programer)方法。这下麻烦多了。如果还有C程序员,C
++程序员,JAVA程序员呢。麻烦大了!

但是如果改用接口,就完全不一样了:
首先声明一个程序员接口:
interface IProgramer()
{
  WriteCode();
}

然后声明两个类,并实现IProgramer接口:
class clsVBProgramer():IProgramer
{
  ....
  WriteCode()
  
{
     
//用VB语言写代码;
  }

  ....
}


class clsDelphiProgramer():IProgramer
{
  ....
  WriteCode()
  
{
    
//用Delphi语言写代码;
  }

   ....
}

对clsProject这个类进行一下修改:
class clsProject()
{
  ....
  WritePrograme(IProgramer programer)
  
{
    programer.WriteCode();
//写代码
  }

  ......
}


main()
{
   clsProject proj
=new  clsProject;
   IProgramer programer;
   
//如果需要用VB写代码
   programer=new clsVBProgramer;
   proj.WritePrograme(programer);
   
//如果需要用Delphi写代码
   programer=new clsDelphiProgramer;
   proj.WritePrograme(programer);    
}

如果再有C#,C,C
++,JAVA这样的程序员添加进来的话,我们只需把它们相关的类加进来,然后在main()中稍做修改就OK了。扩充性特别好!

另外我们如果把clsProject这个类封成一个组件,那么当我们的用户需要要扩充功能的时候,我们只需要在外部做很小的修改就能实现,可以说根本就用不着改动我们已经封好组件!是不是很方便,很强大! 

 

但是同样也可以用以下方法实现

使用基类
class clsProgramer
{
     
public abstract WriteCode()
     
{
      }

}


class clsDelphiProgramer():clsProgramer
{
  ....
  
public override WriteCode()
  
{
    
//用Delphi语言写代码;
  }

   ....
}


也可以