逐步解說:建立和實作介面 (Visual Basic)

介面描述屬性、方法和事件的特性,但將實作詳細資料交給結構或類別來說明。

本逐步解說會示範如何宣告和實作介面。

注意

本逐步解說不會提供如何建立使用者介面的資訊。

注意

在下列指示的某些 Visual Studio 使用者介面項目中,您的電腦可能會顯示不同的名稱或位置: 您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。 如需詳細資訊,請參閱將 Visual Studio IDE 個人化

定義介面

  1. 開啟新的 Visual Basic Windows 應用程式專案。

  2. 按一下 [專案] 功能表上的 [新增模組],將新的模組新增至專案。

  3. 將新模組命名為 Module1.vb,然後按一下 [新增]。 新模組的程式碼隨即顯示。

  4. ModuleEnd Module 陳述式之間鍵入 Interface TestInterface,然後按 ENTER,在 Module1 內定義名為 TestInterface 的介面。 程式碼編輯器會縮排 Interface 關鍵字,並新增 End Interface 陳述式以形成程式碼區塊。

  5. InterfaceEnd Interface 陳述式之間放置下列程式碼,以定義介面的屬性、方法和事件:

    Property Prop1() As Integer
    Sub Method1(ByVal X As Integer)
    Event Event1()
    

實作

您可能會注意到,用來宣告介面成員與用來宣告類別成員的語法不同。 這項差異反映了介面無法包含實作程式碼的事實。

實作介面

  1. 將下列陳述式新增至 Module1,其在 End Interface 陳述式之後,但在 End Module 陳述式之前,然後按 ENTER,以新增名為 ImplementationClass 的類別:

    Class ImplementationClass
    

    如果您在整合式開發環境中工作,則程式碼編輯器會在您按下 ENTER 時提供相符的 End Class 陳述式。

  2. 將下列 Implements 陳述式新增至 ImplementationClass,其可命名類別所實作的介面:

    Implements TestInterface
    

    Implements 陳述式與類別或結構頂端的其他項目分開列出時,表示類別或結構會實作介面。

    如果您在整合式開發環境中工作,則程式碼編輯器會在您按下 ENTER 時實作 TestInterface 所需的類別成員,且您可以略過下一個步驟。

  3. 如果您並非在整合式開發環境中工作,則必須實作介面 MyInterface 的所有成員。 將下列程式碼新增至 ImplementationClass 以實作 Event1Method1Prop1

    Event Event1() Implements TestInterface.Event1
    
    Public Sub Method1(ByVal X As Integer) Implements TestInterface.Method1
    End Sub
    
    Public Property Prop1() As Integer Implements TestInterface.Prop1
        Get
        End Get
        Set(ByVal value As Integer)
        End Set
    End Property
    

    Implements 陳述式會命名要實作的介面和介面成員。

  4. 將私用欄位新增至儲存屬性值的類別,以完成 Prop1 的定義:

    ' Holds the value of the property.
    Private pval As Integer
    

    從屬性 “get accessor” 傳回 pval 的值。

    Return pval
    

    在屬性 “set accessor” 中設定 pval 的值。

    pval = value
    
  5. 藉由新增下列程式碼來完成 Method1 的定義。

    MsgBox("The X parameter for Method1 is " & X)
    RaiseEvent Event1()
    

測試介面的實作

  1. 以滑鼠右鍵按一下 [方案總管] 中專案的啟動表單,然後按一下 [檢視程式碼]。 編輯器隨即顯示啟動表單的類別。 根據預設,啟動表單稱為 Form1

  2. 將下列 testInstance 欄位新增至 Form1 類別:

    Dim WithEvents testInstance As TestInterface
    

    Form1 類別可藉由將 testInstance 宣告為 WithEvents 來處理其事件。

  3. 將下列事件處理常式新增至 Form1 類別,以處理 testInstance 引發的事件:

    Sub EventHandler() Handles testInstance.Event1
        MsgBox("The event handler caught the event.")
    End Sub
    
  4. 將名為 Test 的副程式新增至 Form1 類別,以測試實作類別:

    Sub Test()
        '  Create an instance of the class.
        Dim T As New ImplementationClass
        ' Assign the class instance to the interface.
        ' Calls to the interface members are 
        ' executed through the class instance.
        testInstance = T
        ' Set a property.
        testInstance.Prop1 = 9
        ' Read the property.
        MsgBox("Prop1 was set to " & testInstance.Prop1)
        '  Test the method and raise an event.
        testInstance.Method1(5)
    End Sub
    

    Test 程序會建立實作 MyInterface 的類別執行個體,將該執行個體指派給 testInstance 欄位、設定屬性,以及透過介面執行方法。

  5. 新增程式碼,以從啟動表單的 Form1 Load 程序呼叫 Test 程序:

    Private Sub Form1_Load(ByVal sender As System.Object,
                           ByVal e As System.EventArgs) Handles MyBase.Load
        Test() ' Test the class.
    End Sub
    
  6. 透過按下 F5 執行 Test 程序。 「Prop1 已設定為 9」訊息隨即顯示。 按一下 [確定] 之後,會顯示「Method1 的 X 參數為 5」訊息。 按一下 [確定],「事件處理常式攔截到事件」訊息隨即顯示。

另請參閱