• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • C# 語言規范--1.9 接口

    發表于:2007-07-14來源:作者:點擊數: 標簽:
    C# 語言規范--1.9 接口: 一個接口定義一個協定。實現接口的類或結構必須遵守其協定。接口可以包含方法、屬性、索引器和事件作為成員。 示例 interface IExample{ string this[int index] { get; set; } event EventHandler E; void F(int value); string P
    C# 語言規范--1.9 接口:

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

    示例

    interface IExample{   string this[int index] { get; set; }   event EventHandler E;   void F(int value);   string P { get; set; }}public delegate void EventHandler(object sender, EventArgs e);

    顯示了一個包含索引器、事件 E、方法 F 和屬性 P 的接口。

    接口可以使用多重繼承。在下面的示例中,

    interface IControl{   void Paint();}interface ITextBox: IControl{   void SetText(string text);}interface IListBox: IControl{   void SetItems(string[] items);}interface IComboBox: ITextBox, IListBox {}

    接口 IComboBox 同時從 ITextBoxIListBox 繼承。

    類和結構可以實現多個接口。在下面的示例中,

    interface IDataBound{   void Bind(Binder b);}public class EditBox: Control, IControl, IDataBound{   public void Paint() {...}   public void Bind(Binder b) {...}} 

    EditBox 從類 Control 派生,并且同時實現 IControlIDataBound。

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

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

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

    class Test{   static void Main() {      EditBox editbox = new EditBox();      editbox.Paint();   // error: no such method      IControl control = editbox;      control.Paint();   // calls EditBox's Paint implementation   }}

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>