在C#的开发中,接口是非常重要也非常好用的。可是很多时候很多人都不是很了解接口的做用,以及该如何使用。下面我们就来理解接口的作用,并看看如何使用吧。
假设我们公司有两种程序员:VB程序员,指的是用VB写程序的程序员,用clsVBProgramer这个类表示;Delphi程序员指的是用 Delphi写程序的程序员,用clsDelphiProgramer这个类来表示。 每个类都有一个WriteCode()方法。定义如下:
但是如果这时公司又来了一个C#程序员,我们怎么改这段程序,使它能够实现用C#写程序的功能呢?我们需要增加一个新类 clsCSharpProgramer,同时在此clsProject这个类中要再次重载WritePrograme (clsCSharpProgramer programer)方法。这下麻烦多了。如果还有C程序员,C
++
程序员,JAVA程序员呢。麻烦大了!

但是如果改用接口,就完全不一样了:
首先声明一个程序员接口:
如果再有C#,C,C
++
,JAVA这样的程序员添加进来的话,我们只需把它们相关的类加进来,然后在main()中稍做修改就OK了。扩充性特别好!像下面这样添加一个CSharp程序员添加进来:
假设我们公司有两种程序员: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();
}
然后声明两个类,并实现IProgramer接口:
{
WriteCode();
}
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);
}