av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

C#實現(xiàn)多個接口淺析

本文向大家介紹C#實現(xiàn)多個接口,可能好多人還不知道C#實現(xiàn)多個接口,沒有關系,看完本文你肯定有不少收獲,希望本文能教會你更多東西。

一個接口定義一個協(xié)定。C#實現(xiàn)多個接口的類或結構必須遵守其協(xié)定。接口可以包含方法、屬性、索引器和事件作為成員。

 
 
 
  1. interface IExample  
  2. {  
  3.    string this[int index] { get; set; }  
  4.    event EventHandler E;  
  5.    void F(int value);  
  6.    string P { get; set; }  
  7. }  
  8. public delegate void EventHandler(object sender, EventArgs e); 

顯示了一個包含索引器、事件 E、方法 F 和屬性 P 的接口。接口可以使用多重繼承。在下面的示例中,

 
 
 
  1. interface IControl  
  2. {  
  3.    void Paint();  
  4. }  
  5. interface ITextBox: IControl  
  6. {  
  7.    void SetText(string text);  
  8. }  
  9. interface IListBox: IControl  
  10. {  
  11.    void SetItems(string[] items);  
  12. }  
  13. interface IComboBox: ITextBox, IListBox {} 

接口 IComboBox 同時從 ITextBox 和 IListBox 繼承。類和結構可以實現(xiàn)多個接口。在下面的示例中,

 
 
 
  1. interface IDataBound  
  2. {  
  3.    void Bind(Binder b);  
  4. }  
  5. public class EditBox: Control, IControl, IDataBound  
  6. {  
  7.    public void Paint() {...}  
  8.    public void Bind(Binder b) {...}  

類 EditBox 從類 Control 派生,并且同時實現(xiàn) IControl 和 IDataBound。

在前面的示例中,IControl 接口中的 Paint 方法和 IDataBound 接口中的 Bind 方法是使用 EditBox 類的公共成員實現(xiàn)的。C# 提供了另一種方式來實現(xiàn)這些方法,使得實現(xiàn)類避免將這些成員設置成公共的。這就是:接口成員可以用限定名來實現(xiàn)。例如,在 EditBox 類中將 Paint 方法命名為 IControl.Paint,將 Bind 方法命名為 IDataBound.Bind 方法。

 
 
 
  1. public class EditBox: IControl, IDataBound  
  2. {  
  3.    void IControl.Paint() {...}  
  4.    void IDataBound.Bind(Binder b) {...}  

用這種方式C#實現(xiàn)多個接口成員稱為顯式接口成員,這是因為每個成員都顯式地指定要實現(xiàn)的接口成員。顯式接口成員只能通過接口來調用。例如,在 EditBox 中實現(xiàn)的 Paint 方法只能通過強制轉換為 IControl 接口來調用。

 
 
 
  1. class Test  
  2. {  
  3.    static void Main() {  
  4.       EditBox editbox = new EditBox();  
  5.       editbox.Paint();   // error: no such method  
  6.       IControl control = editbox;  
  7.       control.Paint();   // calls EditBox's Paint implementation  
  8.    }  

【編輯推薦】

  1. C# 3.0編譯器簡單介紹
  2. C#使用函數(shù)重載學習筆記
  3. Visual C#對數(shù)據(jù)庫處理概述
  4. C#具有隱式類型聲明描述
  5. C#使用SharpZipLib分析

網(wǎng)頁標題:C#實現(xiàn)多個接口淺析
URL鏈接:http://uogjgqi.cn/article/dpjioee.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流