Implements — Instrukcja

Określa co najmniej jeden interfejs lub elementy członkowskie interfejsu, które muszą być zaimplementowane w definicji klasy lub struktury, w której się pojawia.

Składnia

Implements interfacename [, ...]  
' -or-  
Implements interfacename.interfacemember [, ...]  

generatora

interfacename
Wymagany. Interfejs, którego właściwości, procedury i zdarzenia mają być implementowane przez odpowiednie elementy członkowskie w klasie lub strukturze.

interfacemember
Wymagany. Element członkowski wdrażanego interfejsu.

Uwagi

Interfejs to kolekcja prototypów reprezentujących elementy członkowskie (właściwości, procedury i zdarzenia) hermetyzowane interfejsu. Interfejsy zawierają tylko deklaracje dla elementów członkowskich; klasy i struktury implementują te elementy członkowskie. Aby uzyskać więcej informacji, zobacz Interfejsy.

Instrukcja Implements musi natychmiast postępować zgodnie z instrukcją Class or Structure .

Podczas implementowania interfejsu należy zaimplementować wszystkie elementy członkowskie zadeklarowane w interfejsie. Pominięcie dowolnego elementu członkowskiego jest uważane za błąd składniowy. Aby zaimplementować pojedynczy element członkowski, należy określić słowo kluczowe Implements (które jest oddzielone od Implements instrukcji) podczas deklarowania składowej w klasie lub strukturze. Aby uzyskać więcej informacji, zobacz Interfejsy.

Klasy mogą używać prywatnych implementacji właściwości i procedur, ale te elementy członkowskie są dostępne tylko przez rzutowanie wystąpienia klasy implementowania do zmiennej zadeklarowanej jako typu interfejsu.

Przykład 1

W poniższym przykładzie pokazano, jak używać instrukcji Implements do implementowania elementów członkowskich interfejsu. Definiuje interfejs o nazwie ze ICustomerInfo zdarzeniem, właściwością i procedurą. Klasa customerInfo implementuje wszystkie elementy członkowskie zdefiniowane w interfejsie.

Public Interface ICustomerInfo
    Event UpdateComplete()
    Property CustomerName() As String
    Sub UpdateCustomerStatus()
End Interface

Public Class customerInfo
    Implements ICustomerInfo
    ' Storage for the property value.
    Private customerNameValue As String
    Public Event UpdateComplete() Implements ICustomerInfo.UpdateComplete
    Public Property CustomerName() As String _
        Implements ICustomerInfo.CustomerName
        Get
            Return customerNameValue
        End Get
        Set(ByVal value As String)
            ' The value parameter is passed to the Set procedure
            ' when the contents of this property are modified.
            customerNameValue = value
        End Set
    End Property

    Public Sub UpdateCustomerStatus() _
        Implements ICustomerInfo.UpdateCustomerStatus
        ' Add code here to update the status of this account.
        ' Raise an event to indicate that this procedure is done.
        RaiseEvent UpdateComplete()
    End Sub
End Class

Należy pamiętać, że klasa customerInfo używa instrukcji Implements w osobnym wierszu kodu źródłowego, aby wskazać, że klasa implementuje wszystkie elementy członkowskie interfejsu ICustomerInfo . Następnie każdy element członkowski w klasie używa Implements słowa kluczowego w ramach deklaracji składowej, aby wskazać, że implementuje ten element członkowski interfejsu.

Przykład 2

W poniższych dwóch procedurach pokazano, jak można użyć interfejsu zaimplementowanego w poprzednim przykładzie. Aby przetestować implementację, dodaj te procedury do projektu i wywołaj procedurę testImplements .

Public Sub TestImplements()
    ' This procedure tests the interface implementation by
    ' creating an instance of the class that implements ICustomerInfo.
    Dim cust As ICustomerInfo = New customerInfo()
    ' Associate an event handler with the event that is raised by
    ' the cust object.
    AddHandler cust.UpdateComplete, AddressOf HandleUpdateComplete
    ' Set the CustomerName Property
    cust.CustomerName = "Fred"
    ' Retrieve and display the CustomerName property.
    MsgBox("Customer name is: " & cust.CustomerName)
    ' Call the UpdateCustomerStatus procedure, which raises the
    ' UpdateComplete event.
    cust.UpdateCustomerStatus()
End Sub

Sub HandleUpdateComplete()
    ' This is the event handler for the UpdateComplete event.
    MsgBox("Update is complete.")
End Sub

Zobacz też