接口实现示例
实现接口的类必须实现其所有的属性、方法和事件。
以下示例定义了两个接口。第二个接口 Interface2 对 Interface1 做了继承,且定义了一个附加属性和方法。
Interface Interface1
Sub sub1(ByVal i As Integer)
End Interface
Interface Interface2
Inherits Interface1 ' Demonstrates interface inheritance.
Sub M1(ByVal y As Integer)
ReadOnly Property Num() As Integer
End Interface
下一个示例则实现上一示例中所定义的接口 Interface1:
Public Class ImplementationClass1
Implements Interface1
Sub Sub1(ByVal i As Integer) Implements Interface1.Sub1
' Place code here to implement this method.
End Sub
End Class
最后一个示例则实现 Interface2,包括自 Interface1 继承的方法:
Public Class ImplementationClass2
Implements Interface2
Dim INum As Integer = 0
Sub sub1(ByVal i As Integer) Implements Interface2.Sub1
' Place code here that implements this method.
End Sub
Sub M1(ByVal x As Integer) Implements Interface2.M1
' Place 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