Compartilhar via


Exemplos de implementação de Interface em Visual Basic

As classes que implementam uma interface devem implementar todas as suas propriedades, métodos e eventos.

O exemplo a seguir define duas interfaces.A segunda interface, Interface2, herda Interface1 e define um método e propriedade adicionais.

Interface Interface1
    Sub sub1(ByVal i As Integer)
End Interface

' Demonstrates interface inheritance.
Interface Interface2
    Inherits Interface1
    Sub M1(ByVal y As Integer)
    ReadOnly Property Num() As Integer
End Interface

O próximo exemplo implementa Interface1, a interface definida no exemplo anterior:

Public Class ImplementationClass1
    Implements Interface1
    Sub Sub1(ByVal i As Integer) Implements Interface1.sub1
        ' Insert code here to implement this method.
    End Sub
End Class

O exemplo final implementa Interface2, incluindo um método herdado de Interface1:

Public Class ImplementationClass2
    Implements Interface2
    Dim INum As Integer = 0
    Sub sub1(ByVal i As Integer) Implements Interface2.sub1
        ' Insert code here that implements this method.
    End Sub
    Sub M1(ByVal x As Integer) Implements Interface2.M1
        ' Insert code here to implement this method.
    End Sub

    ReadOnly Property Num() As Integer Implements _
       Interface2.Num
        Get
            Num = INum
        End Get
    End Property
End Class

Consulte também

Tarefas

Como: Criar e implementar interfaces

Demonstra Passo a passo: Criar e implementar interfaces

Conceitos

Visão geral de interfaces

Definição de Interface

Palavra-chave Implements e a demonstrativo Implements

Quando usar interfaces

Referência

Declaração Interface (Visual Basic)

Implementa Declaração