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

推荐订阅源

H
Heimdal Security Blog
A
Arctic Wolf
K
Kaspersky official blog
V
Vulnerabilities – Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
L
LINUX DO - 热门话题
MongoDB | Blog
MongoDB | Blog
T
Threat Research - Cisco Blogs
D
Docker
爱范儿
爱范儿
T
Tenable Blog
C
Check Point Blog
B
Blog
C
Cisco Blogs
Vercel News
Vercel News
The Cloudflare Blog
T
Threatpost
NISL@THU
NISL@THU
T
Tor Project blog
V2EX - 技术
V2EX - 技术
P
Palo Alto Networks Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tailwind CSS Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
SecWiki News
SecWiki News
博客园 - 司徒正美
S
Security @ Cisco Blogs
GbyAI
GbyAI
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
The Register - Security
The Register - Security
Recorded Future
Recorded Future
Cloudbric
Cloudbric
Webroot Blog
Webroot Blog
N
News and Events Feed by Topic
Y
Y Combinator Blog
博客园_首页
T
Troy Hunt's Blog
The Hacker News
The Hacker News
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
U
Unit 42
AWS News Blog
AWS News Blog
PCI Perspectives
PCI Perspectives
V
Visual Studio Blog
博客园 - 聂微东
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 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语言写代码;
  }

   ....
}


也可以