收藏 分销(赏)

C#接口实例应用的的深入探讨.doc

上传人:s4****5z 文档编号:8735234 上传时间:2025-02-28 格式:DOC 页数:4 大小:43KB 下载积分:10 金币
下载 相关 举报
C#接口实例应用的的深入探讨.doc_第1页
第1页 / 共4页
C#接口实例应用的的深入探讨.doc_第2页
第2页 / 共4页


点击查看更多>>
资源描述
C#接口实例应用的的深入探讨 interface IPrint { void Print(); } class A : IPrint { public void Print() { System.Console.WriteLine("A"); } } class B : IPrint { public void Print() { System.Console.WriteLine("B"); } } class AppMain { public static void Print(IPrint obj) { obj.Print(); } public static void Main() { Print(new A()); // display A Print(new B()); // display B } } 在这个例子中,IPrint 接口中定义了Print方法,但没有实现方法体. 类A和类B都继承了IPrint接口,并且实现了不同的Print方法. 在AppMain 中调用A\B,获得不同的打印结果 C#接口实例应用问题的提出,假设我们公司有两种程序员:VB程序员,指的是用VB写程序的程序员,用clsVBProgramer这个类表示;Delphi程序员指的是用Delphi写程序的程序员,用clsDelphiProgramer这个类来表示。每个类都有一个WriteCode()方法。 C#接口实例定义如下: 1. class clsVBProgramer()   2. {   3. ....   4. WriteCode()   5. {   6.      //用VB语言写代码;   7. }   8. ....   9. }   10.   11. class clsDelphiProgramer()   12. {   13. ....   14. WriteCode()   15. {   16.     //用Delphi语言写代码;   17. }   18.    ....   19. }   现在公司来了一个项目,要求派某个程序员写一个程序。 C#接口实例 20. class clsProject()   21. {   22. ....   23. WritePrograme(clsVBProgramer programer)//用VB写代码   24. {   25.     programer.WriteCode();   26. }   27. WritePrograme(clsDelphiProgramer programer)   28. //重载方法,用Delphi写代码   29. {   30.     programer.WriteCode();   31. }   32. ......   33. }   在主程序中我们可以这样写: C#接口实例: 34. main()   35. {   36.    clsProject proj=new clsProject;   37.    //如果需要用VB写代码   38.    clsVBProgramer programer1=new clsVBProgramer;   39.    proj.WritePrograme(programer1);   40.    //如果需要用Delphi写代码   41.    clsDelphiProgramer programer2=new clsDelphiProgramer;   42.    proj.WritePrograme(programer2);   43. }  但是如果这时公司又来了一个C#程序员,我们怎么改这段程序,使它能够实现用C#写程序的功能呢?我们需要增加一个新类clsCSharpProgramer,同时在此clsProject这个类中要再次重载WritePrograme(clsCSharpProgramer programer)方法。这下麻烦多了。如果还有C程序员,C++程序员,JAVA程序员呢。麻烦大了! 但是如果改用接口,就完全不一样了,首先声明一个程序员接口: C#接口实例: 44. interface IProgramer()   45. {   46. WriteCode();   47. }  然后声明两个类,并实现IProgramer接口: 48. class clsVBProgramer():IProgramer   49. {   50. ....   51. WriteCode()   52. {   53.      //用VB语言写代码;   54. }   55. ....   56. }   57.   58. class clsDelphiProgramer():IProgramer   59. {   60. ....   61. WriteCode()   62. {   63.     //用Delphi语言写代码;   64. }   65.    ....   66. }   67. 对clsProject这个类进行一下修改:   68. class clsProject()   69. {   70. ....   71. WritePrograme(IProgramer programer)   72. {   73.     programer.WriteCode();//写代码   74. }   75. ......   76. }   77.   78. main()   79. {   80.    clsProject proj=new clsProject;   81.    IProgramer programer;   82.    //如果需要用VB写代码   83.    programer=new clsVBProgramer;   84.    proj.WritePrograme(programer);   85.    //如果需要用Delphi写代码   86.    programer=new clsDelphiProgramer;   87.    proj.WritePrograme(programer);      88. }   如果再有C#,C,C++,JAVA这样的程序员添加进来的话,我们只需把它们相关的类加进来,然后在main()中稍做修改就OK了。扩充性特别好! 另外我们如果把clsProject这个类封成一个组件,那么当我们的用户需要要扩充功能的时候,我们只需要在外部做很小的修改就能实现,可以说根本就用不着改动我们已经封好组件!
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服