C#接口Interface

 

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

[attributes] [modifiers] interface identifier [: base-list] {interface-body} {;}

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

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

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

执行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值