Ejemplos de implementación de interfaces en Visual Basic

Actualización: noviembre 2007

Las clases que implementan una interfaz deben implementar todas sus propiedades, métodos y eventos.

El ejemplo siguiente define dos interfaces. La segunda interfaz, Interface2, hereda Interface1 y define un método y una propiedad adicional.

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

El ejemplo siguiente implementa Interface1, la interfaz definida en el ejemplo 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

El ejemplo final implementa Interface2, incluyendo un método heredado 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

Vea también

Tareas

Cómo: Crear e implementar interfaces

Tutorial: Crear e implementar interfaces

Conceptos

Información general sobre interfaces

Definición de interfaz

Palabra clave Implements e instrucción Implements

Cuándo se deben utilizar interfaces

Referencia

Instrucción Interface (Visual Basic)

Implements (Instrucción)