Implements 문

표시되는 클래스 또는 구조체 정의에 구현해야 하는 인터페이스 또는 인터페이스 멤버를 하나 이상 지정합니다.

구문

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

부분

interfacename
필수입니다. 클래스 또는 구조체의 해당 멤버가 속성, 프로시저 및 이벤트를 구현할 인터페이스입니다.

interfacemember
필수입니다. 구현되는 인터페이스의 멤버입니다.

설명

인터페이스는 인터페이스가 캡슐화하는 멤버(속성, 프로시저 및 이벤트)를 나타내는 프로토타입 컬렉션입니다. 인터페이스에는 멤버에 대한 선언만 포함됩니다. 클래스 및 구조체는 이러한 멤버를 구현합니다. 자세한 내용은 인터페이스를 참조하세요.

Implements 문은 Class 또는 Structure 문을 즉시 따라야 합니다.

인터페이스를 구현할 때 인터페이스에 선언된 모든 멤버를 구현해야 합니다. 멤버를 생략하는 것은 구문 오류로 간주됩니다. 개별 멤버를 구현하려면 클래스 또는 구조체에서 멤버를 선언할 때 Implements 키워드(Implements 문과는 별개)를 지정합니다. 자세한 내용은 인터페이스를 참조하세요.

클래스는 속성 및 프로시저의 Private 구현을 사용할 수 있지만 이러한 멤버는 구현 클래스의 인스턴스를 인터페이스 형식으로 선언된 변수로 캐스팅해야만 액세스할 수 있습니다.

예 1

다음 예제에서는 Implements 문을 사용하여 인터페이스의 멤버를 구현하는 방법을 보여줍니다. 이벤트, 속성 및 프로시저를 사용하여 ICustomerInfo라는 인터페이스를 정의합니다. 클래스 customerInfo는 인터페이스에 정의된 모든 멤버를 구현합니다.

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

클래스 customerInfo는 별도의 소스 코드 줄에 있는 Implements 문을 사용하여 클래스가 ICustomerInfo 인터페이스의 모든 멤버를 구현함을 나타냅니다. 그런 다음 클래스의 각 멤버는 Implements 키워드를 멤버 선언의 일부로 사용하여 해당 인터페이스 멤버를 구현함을 나타냅니다.

예제 2

다음 두 프로시저에서는 앞의 예제에서 구현된 인터페이스를 사용하는 방법을 보여 줍니다. 구현을 테스트하려면 이러한 프로시저를 프로젝트에 추가하고 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

참고 항목