欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺析c# 接口

 更新時(shí)間:2020年07月17日 15:51:40   作者:莫得感情的代碼機(jī)器  
這篇文章主要介紹了c# 接口的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下。

接口:

是指定一組函數(shù)成員而不是實(shí)現(xiàn)他們的引用類型。所以只能類喝啊結(jié)構(gòu)來(lái)實(shí)現(xiàn)接口,在結(jié)成該接口的類里面必須要實(shí)現(xiàn)接口的所有方法

接口的特點(diǎn):

繼承于接口的類,必須要實(shí)現(xiàn)所有的接口成員

類可以繼承,但是類只能繼承一個(gè)基類,但是類可以繼承多個(gè)接口

接口接口的定義用interface關(guān)鍵字,后面加接口的名稱,名稱通常是以字母I開(kāi)頭,接口不需要訪問(wèn)修符,因?yàn)榻涌诙际枪┩獠空{(diào)用的,所以都是public的接口定義了所有類集成接口時(shí)應(yīng)該應(yīng)該遵循的語(yǔ)法合同,接口里面的內(nèi)容是語(yǔ)法合同中“是什么”的部分,繼承與接口的派生類中定義的是語(yǔ)法合同中“怎么做”的部分,接口中,只定義接口成員的聲明,成員包括屬性、方法、事件等。

因此在定義接口時(shí)候要注意如下幾點(diǎn):

  • 1,接口聲明不能包含以下成員:數(shù)據(jù)成員,靜態(tài)成員
  • 2,接口聲明只能包含如下類型的非靜態(tài)成員函數(shù)的聲明:方法、屬性、事件、索引器。
  • 3,這些函數(shù)成員的聲明不能包含任何實(shí)現(xiàn)代碼,而且在每一個(gè)成員聲明的主題后必須使用分號(hào)。

1,例子;

//定義一個(gè)接口IParentInterface
    interface IParentInterface {

      void ParentInterface();//聲明接口成員
    }

    class AllInterface : IParentInterface
    {

      public void ParentInterface() {

        Console.WriteLine("Hello");
      }
    }
    static void Main(string[] args)
    {
      AllInterface all = new AllInterface();
      all.ParentInterface();
    }

實(shí)現(xiàn)結(jié)果:

2,如果一個(gè)接口繼承其他接口,那么實(shí)現(xiàn)類或結(jié)構(gòu)就需要實(shí)現(xiàn)所有接口的成員

//定義一個(gè)接口IParentInterface
    interface IParentInterface {

      void ParentInterface();//聲明接口成員
    }
    //IChildInterface
    interface IChildInterface {

      void ChildInterface();//聲明接口成員
    }

    class AllInterface : IChildInterface
    {

      public void ParentInterface() {

        Console.Write("Hello" + " ");
      }

      public void ChildInterface() {

        Console.WriteLine("World");
      }
    }
    static void Main(string[] args)
    {
      AllInterface all = new AllInterface();
      all.ParentInterface();
      all.ChildInterface();
    }

實(shí)現(xiàn)結(jié)果:

以上就是淺析c# 接口的詳細(xì)內(nèi)容,更多關(guān)于c# 接口的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論