C#之理解接口的作用
在C#的开发中,接口是非常重要也非常好用的。可是很多时候很多人都不是很了解接口的做用,以及该如何使用。下面我们就来理解接口的作用,并看看如何使用吧。
假设我们公司有两种程序员:VB程序员,指的是用VB写程序的程序员,用clsVBProgramer这个类表示;Delphi程序员指的是用 Delphi写程序的程序员,用clsDelphiProgramer这个类来表示。 每个类都有一个WriteCode()方法。定义如下:
假设我们公司有两种程序员:VB程序员,指的是用VB写程序的程序员,用clsVBProgramer这个类表示;Delphi程序员指的是用 Delphi写程序的程序员,用clsDelphiProgramer这个类来表示。 每个类都有一个WriteCode()方法。定义如下:
class
clsVBProgramer()
{
WriteCode()
{
// 用VB语言写代码;
}
}
class clsDelphiProgramer()
{
WriteCode()
{
// 用Delphi语言写代码;
}
}
{
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);
}
{
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);
}




interface
IProgramer()
{
WriteCode();
}
{
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);
}
{
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);
}

//
clsProject类和IProgramer都无须改变,只需要再写一个CSharp程序员的类就可以了
class clsCSharpProgramer():IProgramer // 记住,要实现IProgramer接口
{
WriteCode()
{
// 用CSharp语言写代码;
}
}
// 在使用的时候如下:
main()
{
clsProject proj = new clsProject;
IProgramer programer;
// 如果需要用CSharp写代码
programer = new clsCSharpProgramer;
proj.WritePrograme(programer);
}
class clsCSharpProgramer():IProgramer // 记住,要实现IProgramer接口
{
WriteCode()
{
// 用CSharp语言写代码;
}
}
// 在使用的时候如下:
main()
{
clsProject proj = new clsProject;
IProgramer programer;
// 如果需要用CSharp写代码
programer = new clsCSharpProgramer;
proj.WritePrograme(programer);
}
这样我们如果把clsProject这个类封成一个组件,那么当我们的用户需要要扩充功能的时候,我们只需要在外部做很小的修改就能实现,可以说根本就用不着改动我们已经封好组件!是不是很方便,很强大!
转自依依园地博客园博客原文链接
http://www.cnblogs.com/lynnlin/archive/2008/06/25/1229928.html