zoukankan      html  css  js  c++  java
  • 接口知识点

    接口(interface)知识点

    1.接口的定义和声明

      接口:是指一组函数成员而不实现它们的引用类类型。所以,只能类和结构来实现接口。

      接口声明必须注意几点:

      @1.必须在基类列表后面列出接口名称;

      @2.必须为接口的每一个成员提供实现。

      @3.接口声明不能包括:数据成员和静态成员。

      @4.接口声明可以有任何访问修饰符:public,private,internal,protected。

      @5.接口的成员是隐式public,不允许有任何访问修饰符,包括public。

      @6.按照惯例,接口名称必须从大写I开始。

      @7.必须有关键字:interface。

      @8.接口成员不能包含任何实现代码,每一个成员声明的主体后必须使用分号。只有类和结构才能实现接口。

      @9.如果从基类继承并实现了接口,基类列表中的基类名称必须放在所有接口之前。

    2.代码例子:声明接口,对对象数组进行排序。

      class MyClass : IComparable
      {
        public int TheValue;
        public int CompareTo(object obj)
        {
          MyClass mc = (MyClass)obj;
          if (this.TheValue > mc.TheValue) return 1;
          if (this.TheValue < mc.TheValue) return -1;
          return 0;
        }
      }
      class Program
      {
        static void PrintOut(MyClass[] mc )
        {
          foreach (var item in mc)
          Console.WriteLine(item.TheValue);
        }
        static void Main(string[] args)
        {
          var myInt = new int[] { 5 , 8 , 12 , 10};
          MyClass[] mcArr =new MyClass[4] ;
          for (int i = 0; i < 4; i++)
          {
            mcArr[i] = new MyClass();
            mcArr[i].TheValue = myInt[i];
          }
          PrintOut(mcArr);
          Array.Sort(mcArr);
          PrintOut(mcArr);

        }

      }

    3.接口是引用类型和as运算符

      @1.我们不能通过对象成员直接访问接口。我们可以把类对象引用,强制转换为接口类型来引用接口。

      @2.关于接口引用的代码如下:

      interface IInfo
      { void PrintfOut(string s); }
      class MyClass : IInfo
      {
        public void PrintfOut(string s)
        { Console.WriteLine(s); }
      }
      class Program
      {
        static void Main(string[] args)
        {
          MyClass mc = new MyClass();
          mc.PrintfOut("I Love You!");   //接口使用
          IInfo fo = (IInfo)mc;   //通过强制转换使用接口引用
          fo.PrintfOut("I Love You!");
          IInfo Info = mc as IInfo;   //通过as运算符,使用接口引用,功能和强制转换一样
          Info.PrintfOut("I Love You!");

        }

      }

    4.显式接口成员实现

      MyClass : IInfo

      {

        void IInfo.Printf(string s )

        {

          ......

        }

      }

    5.不同类实现一个接口的代码

      class Animal { }
      interface ILiveBirth
      { string BabyCalled();}
      class Cat : Animal, ILiveBirth
      {
        string ILiveBirth.BabyCalled()
        { return "HAHA"; }
      }
      class Dog : Animal, ILiveBirth
      {
        public string BabyCalled()
        { return "LaLa"; }
      }
      class Bird : Animal
      { }
      class Program
      {
      static void Main(string[] args)
      {
        var animalArray = new Animal[3] ;
        animalArray[0] = new Cat();
        animalArray[1] = new Dog();
        animalArray[2] = new Bird();
        foreach (var item in animalArray)
        {
          ILiveBirth b = item as ILiveBirth;
          if (b != null)
          Console.WriteLine(b.BabyCalled());
        }
      }

     

  • 相关阅读:
    第二周作业
    十二周
    十一周
    第十周
    第九周
    第八周
    第七周
    第六周
    实验报告三
    第四周课程总结&试验报告(二)
  • 原文地址:https://www.cnblogs.com/xumaodun/p/4226492.html
Copyright © 2011-2022 走看看