Implements — Instrukcja

Określa jeden lub więcej interfejsów, lub członków interfejsu, które muszą być wdrożone w klasie lub struktury definicji, w której pojawia się.

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

Części

  • interfacename
    Wymagane.Interfejs, którego właściwości, procedur i zdarzenia są realizowane przez odpowiednich członków w klasie lub strukturze.

  • interfacemember
    Wymagane.Członek interfejs, który jest realizowany.

Uwagi

Interfejs jest zbiorem prototypów reprezentujący członków (właściwości, procedur i zdarzenia) hermetyzuje interfejsu.Interfejsy zawierać tylko deklaracje członków; klasy i struktur wdrożenia tych członków.Aby uzyskać więcej informacji, zobacz Interfejsy (Visual Studio).

Implements Instrukcji należy niezwłocznie wykonać Class lub Structure instrukcji.

Gdy implementuje interfejs musi implementować wszystkich członków zadeklarowanych w interfejsie.Pominięcie któregokolwiek członka jest uważana za błąd składni.Aby zaimplementować poszczególnych elementów członkowskich, należy określić Implements — Klauzula (Visual Basic) słowa kluczowego (oddzielonym od Implements instrukcji) kiedy zadeklarować członka klasy lub struktury.Aby uzyskać więcej informacji, zobacz Interfejsy (Visual Studio).

Można użyć klasy Private (Visual Basic) implementacje właściwości i procedur, ale elementy te są dostępne tylko przez Rzutowanie instancji klasy wykonawczych do zmiennej zgłoszone do typu interfejsu.

Przykład

Poniższy przykład pokazuje, jak używać Implements instrukcji w celu implementowania członków interfejsu.Definiuje on interfejsu o nazwie ICustomerInfo z zdarzenie, 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 zauważyć, że klasa customerInfo korzysta z Implements instrukcji na oddzielne źródło wiersza kodu, aby wskazać, że klasa implementuje wszystkich członków ICustomerInfo interfejsu.Następnie używa każdego członka w klasie Implements słowa kluczowego jako część jego deklaracji członka, aby wskazać implementuje tego członka interfejsu.

Dwie następujące procedury pokazują, jak można wykorzystywać interfejs, zaimplementowane w poprzednim przykładzie.Test wprowadzenia w życie, należy dodać do projektu i wywołanie procedury te testImplements procedury.

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ż

Informacje

Implements — Klauzula (Visual Basic)

Interface — Instrukcja (Visual Basic)

Inne zasoby

Interfejsy (Visual Studio)