C# Interface

來源:互聯網
上載者:User

標籤:

1.Interface 定義結構

1 [attributes] [access-modifiers] interface interface-name [:base-inteface-list] 2 { 3        interface body 4 }

[attributes] 性質 (可選項);

[access-modifiers] 存取修飾詞 (可選項);//public  internal  private   protected  protected internal;

interface-name 介面名  (習慣以大寫 I 開頭);

[:base-inteface-list] 介面基類列表 (可選項);

interface body  介面主體(不能有存取修飾詞);//只是定義介面的方法和屬性等的簽名;

                       //實際的實現是寫在使用該介面的class裡;

 

 

1 //Define an Interface2 public interface IStorable3 {4     void Read( ); //不能有access-modifier5     void Write(object);//只是簽名,沒有具體內容6 }

類繼承介面

1 public class Document : IStorable // 繼承IStorable介面2 {3     public void Read( ) {...} //可以有access-modifier4     public void Write(object obj) {...} // 寫出具體的實現內容5     // ...6 }

一個類繼承多個介面

 1 public interface IStorable 2 { 3     void Read( ); 4     void Write(object); 5 } 6  7 interface ICompressible 8 { 9     void Compress();10     void Decompress();11 }12 13 public class Document : IStorable, ICompressible // 繼承多個介面的列表14 {15     //實現IStorable16     public void Read( ) {...} //寫出具體的實現內容17     public void Write(object obj) {...}18     // 實現ICompressible19     public void Compress(){ …}20     public void Decompress() { …}21 }

 

介面繼承介面


interface ICompressible{    void Compress();    void Decompress();}interface ILoggedCompressible : ICompressible  // 介面 ILoggedCompressible繼承 介面ICompressible{    void LogSavedBytes();}public class Document : ILoggedCompressible {    // 實現ICompressible    public void Compress(){ …}    public void Decompress() { …}    // 實現ILoggedCompressible     public void LogSavedBytes() { …}  // 要實現所有的介面成員}

 

 

 

顯式定義與隱式定義

 1  interface IStorable 2  { 3        void Read(); 4        void Write(); 5  } 6  interface ITalk 7  { 8         void Talk(); 9         void Read();10  }11  public class Document : IStorable, ITalk12  {13     public Document(string s)14      {…; }15      // Make read virtual   定義為虛函數16      public virtual void Read() //Istorable的Read方法為 隱式定義17      {…; }            18      public void Write()19      {…; }20      void ITalk.Read()  // ITalk的 Read方法 為 顯式定義  不能有 存取修飾詞  隱式為public21      { …; }22      public void Talk() //不需要顯式定義23      { …; }24 }    

顯式定義與隱式定義的調用

  1 public class Document : IStorable, ITalk

2 {
 3     public void Read() 4      { …; }     … 5     void ITalk.Read() 6      { …; } 7 } 8  9  Document theDoc = new Document(“Test”);10 theDoc.Read();   //調用隱式定義的Read()
11 12 ITalk itDoc = theDoc; // 轉換為interface13 itDoc.Read(); //調用顯式定義的Read()

 

C# Interface

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.