基礎_C# 介面

來源:互聯網
上載者:User
C# 介面

介面是使用 interface 關鍵字定義的。例如:

interface IComparable  {      int CompareTo(object obj);  }  

介面描述可屬於任何結構的一組相關行為。介面可由方法、屬性、事件、索引器或這四種成員類型的任何組合構成。介面不能包含欄位。介面成員一定是公用(public)的。

類和結構可以像類繼承基類或結構一樣從介面繼承,但有兩個例外:

  • 類或結構可繼承多個介面

  • 當類或結構繼承介面時,它繼承成員定義但不繼承實現。

若要實現介面成員,類中的對應成員必須是公用的非靜態,並且與介面成員具有相同的名稱和簽名。

類的屬性和索引器可以為介面上定義的屬性或索引器定義額外的訪問器。例如,介面可以聲明一個帶有 get 訪問器的屬性,而實現該介面的類可以聲明同時帶有 get 和 set 訪問器的同一屬性。但是,如果屬性或索引器使用顯式實現,則訪問器必須匹配。

介面和介面成員是抽象的;介面不提供預設實現。

介面可以繼承其他介面。類可以通過其繼承的基類或介面多次繼承某個介面。在這種情況下,如果將該介面聲明為新類的一部分,則類只能實現該介面一次。如果沒有將繼承的介面聲明為新類的一部分,其實現將由聲明它的基類提供。基類可以使用虛擬成員實現介面成員;在這種情況下,繼承介面的類可通過重寫虛擬成員來更改介面行為。有關虛擬成員的更多資訊,請參見多態性。

{
ExpandCollapse(sectionToggle0)
}" onkeypress="function onkeypress()
{
ExpandCollapse_CheckKey(sectionToggle0)
}">介面概述

介面可以是命名空間的成員

介面具有下列屬性:

  • 介面類似於抽象基類:繼承介面的任何非抽象類別型都必須實現介面的所有成員。

  • 不能直接執行個體化介面。

  • 介面可以包含事件、索引器、方法和屬性。

  • 介面不包含方法的實現。

  • 類和結構可從多個介面繼承。

  • 介面自身可從多個介面繼承

明確介面實作

如果類實現兩個介面,並且這兩個介面包含具有相同簽名的成員,那麼在類中實現該成員將導致兩個介面都使用該成員作為它們的實現。例如:

interface IControl{    void Paint();}interface ISurface{    void Paint();}class SampleClass : IControl, ISurface{    // Both ISurface.Paint and IControl.Paint call this method.    public void Paint()    {    }}

然而,如果兩個介面成員執行不同的函數,那麼這可能會導致其中一個介面的實現不正確或兩個介面的實現都不正確。可以顯式地實現介面成員 -- 即建立一個僅通過該介面調用並且特定於該介面的類成員。這是使用介面名稱和一個句點命名該類成員來實現的。例如:

public class SampleClass : IControl, ISurface{    void IControl.Paint()    {        System.Console.WriteLine("IControl.Paint");    }    void ISurface.Paint()    {        System.Console.WriteLine("ISurface.Paint");    }}

類成員 IControl.Paint 只能通過 IControl 介面使用,ISurface.Paint 只能通過 ISurface 使用。兩個方法實現都是分離的,都不可以直接在類/類執行個體中使用。例如:

SampleClass obj = new SampleClass();//obj.Paint();  // Compiler error.IControl c = (IControl)obj;c.Paint();  // Calls IControl.Paint on SampleClass.ISurface s = (ISurface)obj;s.Paint(); // Calls ISurface.Paint on SampleClass.

顯式實現還用於解決兩個介面分別聲明具有相同名稱的不同成員(如屬性和方法)的情況:

interface ILeft{      int P { get;}  }  interface IRight{      int P();  }  

為了同時實現兩個介面,類必須對屬性 P 和/或方法 P 使用顯式實現以避免編譯器錯誤。例如:

class Middle : ILeft, IRight  {      public int P() { return 0; }      int ILeft.P { get { return 0; } }  }

介面屬性

public interface ISampleInterface{    // Property declaration:    string Name    {        get;        set;    }}

介面中的索引器

// Indexer on an interface:public interface ISomeInterface{    // Indexer declaration:    int this[int index]    {        get;        set;    }}// Implementing the interface.class IndexerClass : ISomeInterface{    private int[] arr = new int[100];    public int this[int index]   // indexer declaration    {        get        {            // Check the index limits.            if (index < 0 || index >= 100)            {                return 0;            }            else            {                return arr[index];            }        }        set        {            if (!(index < 0 || index >= 100))            {                arr[index] = value;            }        }    }}

在介面中聲明一個事件,然後在類中實現該事件

public delegate void TestDelegate();   // delegate declarationpublic interface ITestInterface{    event TestDelegate TestEvent;    void FireAway();}public class TestClass : ITestInterface{    public event TestDelegate TestEvent;    public void FireAway()    {        if (TestEvent != null)        {            TestEvent();        }    }}public class MainClass{    static private void F()    {        System.Console.WriteLine("This is called when the event fires.");    }    static void Main()    {        ITestInterface i = new TestClass();        i.TestEvent += new TestDelegate(F);        i.FireAway();    }}
顯式實現介面成員
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_csref/html/514cde76-f981-474e-8b40-9493619f899c.htm
使用繼承顯式實現介面成員
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_csref/html/8b402ddc-dff9-4869-89cb-d718c764e68e.htm
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.