1.9 介面
介面定義了一個可由類別和結構實作的規範。介面可以包含方法、屬性、事件和索引子。介面不提供它所定義之成員的實作;它只是指定實作該介面之類別或結構必須提供的成員。
介面可以採用多重繼承。在下面的範例中,介面 IComboBox 同時繼承自 ITextBox 和 IListBox。
interface IControl
{
void Paint();
}
interface ITextBox: IControl
{
void SetText(string text);
}
interface IListBox: IControl
{
void SetItems(string[] items);
}
interface IComboBox: ITextBox, IListBox {}
類別和結構可以實作多個介面。在下面的範例中,類別 EditBox 同時實作 IControl 和 IDataBound。
interface IDataBound
{
void Bind(Binder b);
}
public class EditBox: IControl, IDataBound
{
public void Paint() {...}
public void Bind(Binder b) {...}

當類別或結構實作某個特定介面時,該類別或結構的執行個體可以隱含地轉換成這個介面型別。例如
EditBox editBox = new EditBox();
IControl control = editBox;
IDataBound dataBound = editBox;
在無法確知執行個體是否會實作某個特定執行個體的情況下,您也可以使用動態型別轉換。例如,下列的陳述式使用動態型別轉換,取得物件的 IControl 和 IDataBound 介面實作;因為該物件的實際型別是 EditBox,所以轉換成功。
object obj = new EditBox();
IControl control = (IControl)obj;
IDataBound dataBound = (IDataBound)obj;
在前面的 EditBox 類別中,來自 IControl 介面的 Paint 方法和來自 IDataBound 介面的 Bind 方法使用 public 成員進行實作。C# 也支援明確介面成員實作,類別或結構可以利用這種方式避免讓成員成為 public。明確介面成員實作以完整介面成員的名稱撰寫。例如,EditBox 類別可以用明確介面成員實作,實作 IControl.Paint 和 IDataBound.Bind 方法,如下所示:
public class EditBox: IControl, IDataBound
{
void IControl.Paint() {...}
void IDataBound.Bind(Binder b) {...}
}
明確介面成員只能透過該介面型別存取。例如,前面的 EditBox 類別所提供的 IControl.Paint 實作,必須先將 EditBox 參考轉換成 IControl 介面型別才能叫用。
EditBox editBox = new EditBox();
editBox.Paint(); // Error, no such method
IControl control = editBox;
control.Paint(); // Ok

解决方案 »

  1.   

    举一个最简单的例子来说:
    现在我写了一个Iobject接口,里面就一个方法:ToString()
    这样所有继承了我这个接口的对象都具备了ToString()方法,在各自实现之后,它们便可以通过Iojbect接口来调用ToString()方法.
    如下:public interface Iobject
    {
        public string ToString();
    }public class A:Iobject
    {
        public string ToString()
        {
            return "A";
        }
    }
    public class B:Iobject
    {
        public string ToString()
        {
            return "B";
        }
    }public class main
    {
        public static void Main()
        {
            Iobject a = new A();
            Iobject b = new B();
            System.Windows.Forms.MessageBox.Show(a.ToString());
            System.Windows.Forms.MessageBox.Show(b.ToString());
            //结果将弹出一个"A",一个"B"两个对话框.
        }
    }
      

  2.   


    public   interface   Iobject
    {
            public   string   ToString();
    }public   class   A:Iobject
    {
            public   string   ToString()
            {
                    return   "A";
            }
    }
    public   class   B:Iobject
    {
            public   string   ToString()
            {
                    return   "B";
            }
    }public   class   main
    {
            public   static   void   Main()
            {
                    Iobject   a   =   new   A();
                    Iobject   b   =   new   B();
                    System.Windows.Forms.MessageBox.Show(a.ToString());
                    System.Windows.Forms.MessageBox.Show(b.ToString());
                    //结果将弹出一个"A",一个"B"两个对话框.
            }
    }