打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C# interface学习经验浅谈 - 51CTO.COM
    C# interface学习是我们学习C#语言的一个重点,那么如何掌握好C# interface的使用规则呢?那么本文就向你介绍一些C# interface学习的经验和体会。

     

    C# interface是把所需成员组合起来,以封装一定功能的集合。它好比一个模板,在其中定义了对象必须实现的成员,通过类或结构来实现它。接口不能直接 实例化,即ICount ic=new iCount()是错的。接口不能包含成员的任何代码,只定义成员本身。接口成员的具体代码由实现接口的类提供。接口使用interface关键字进行声 明。声明格式如下:

            
    1. [attributes] [modifiers]   
    2. interface identifier   
    3. [: base-list] {interface-body} {;} 

    C# interface成员的默认访问方式是public,在声明接口成员时不能出现abstract、public、protected、 internal、private、virtual、override或static等关键字。接口成员可以是方法、属性、索引指示器或事件,不能是字 段,而且接口的成员名不能相同。

            
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Text;  
    4.  
    5. namespace Interface  
    6. {  
    7. interface ICount  
    8. {  
    9. void Count();//接口成员的默认访问方式是public  
    10. //int number;//接口中不能定义字段成员  
    11. int para { get;set;}  
    12. }  
    13.  
    14. class Double : ICount  
    15. {  
    16. public void Count()  
    17. //实现ICount的Count()方法  
    18. Console.WriteLine("The double para is {0}",2*para);  
    19. }  
    20. int p;  
    21. public int para  
    22. {  
    23. get { return p; }  
    24. set { p = value; }  
    25. }  
    26. }  
    27.  
    28. class Program  
    29. {  
    30. static void Main(string[] args)  
    31. {  
    32. Double d = new Double();  
    33. d.para = 10;//给"属性"赋值  
    34. d.Count();  
    35.  
    36. ICount ic = (ICount)d;//转换为接口  
    37. ic.para = 5;  
    38. ic.Count();  
    39. Console.ReadLine();  
    40. }  
    41. }  
    42. }  

    C# interface的一点使用总结

    1 一个类可以实现一个以上的接口;

    2 类必须实现接口中的“所有”属性和方法;

    3 属性和方法定义所采用的格式必须与接口定义所采用的格式完全相同。方法所采用的参数数目及参数类型必须与接口中的完全相同。方法的名称也必须相同。

    接口之间的继承:接口的继承仅仅说明了接口之间的继承关系,派生的接口继承了父接口的成员说明,没有继承父接口的实现。private和 internal类型的接口不允许继承。如果派生接口中准备重写父接口的方法,实现方式同类的继承成员的覆盖。

    如果一个类实现了某个接口,即使父接口没有在类的基类表中列出,这个类也隐式地继承了接口的所有父接口。

    如果两个接口A和B含有同名的成员Method,且都由同一个类C实现,则类C必须分别为A和B的Method成员提供单独的实现,即显式实现接口 成员。可行方案:

    (1)直接实现一个接口的成员,显式实现另一个接口的成员;

    (2)显式实现两个接口的成员

            
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Text;  
    4.  
    5. namespace Interface  
    6. {  
    7. interface IEnglish  
    8. {  
    9. float Length();  
    10. float Width();  
    11. }  
    12. interface IMetric  
    13. {  
    14. float Length();  
    15. float Width();  
    16. }  
    17. class Class1 : IEnglish, IMetric  
    18. {  
    19. float lengthInches;  
    20. float widthInches;  
    21. public Class1(float length, float width)  
    22. {  
    23. lengthInches = length;  
    24. widthInches = width;  
    25. }  
    26. //显式实现IEnglish的成员  
    27. float IEnglish.Length()  
    28. {  
    29. return lengthInches;  
    30. }  
    31. float IEnglish.Width()  
    32. {  
    33. return widthInches;  
    34. }  
    35. //显式实现IMetric的成员  
    36. float IMetric.Length()  
    37. {  
    38. return lengthInches * 2.54f;  
    39. }  
    40. float IMetric.Width()  
    41. {  
    42. return widthInches * 2.54f;  
    43. }  
    44.  
    45. static void Main(string[] args)  
    46. {  
    47. Class1 c1 = new Class1(30.0f,20.0f);  
    48. IEnglish e=(IEnglish)c1;  
    49. IMetric m=(IMetric )c1;  
    50. Console.WriteLine("Length(in):{0}",e.Length());  
    51. Console.WriteLine("Width(in):{0}",e.Width());  
    52. Console.WriteLine("Length(cm):{0}",m.Length());  
    53. Console.WriteLine("Width(cm):{0}",m.Width());  
    54. Console.ReadLine();  
    55. }  
    56. }  
    57.  
    58. }  

    执行结果:

    C# interface学习的一些体会和具体的实例演示就向你介绍到这里,希望对你了解和学习C# interface有所帮助。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
矩形
C#中接口的理解
1.1.console接口
对C#中的接口使用和理解
Go语言中反射包的实现原理(The Laws of Reflection)
华为三层交换机s5700配置怎么设置?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服