指定必须在出现的类或结构定义中实现的一个或多个接口或接口成员。
语法
Implements interfacename [, ...]
' -or-
Implements interfacename.interfacemember [, ...]
部件
interfacename
必填。 一个接口,其属性、过程和事件将由类或结构中的相应成员实现。
interfacemember
必填。 正在实现的接口的成员。
注解
接口是一个原型集合,表示接口封装的成员(属性、过程和事件)。 接口仅包含成员的声明;类和结构实现这些成员。 有关详细信息,请参阅 接口。
语句 Implements
必须紧跟 Class
或 Structure
语句。
实现接口时,必须实现接口中声明的所有成员。 省略任何成员被视为语法错误。 若要实现单个成员,请在类或结构中声明成员时指定 Implements 关键字(与语句分开 Implements
)。 有关详细信息,请参阅 接口。
类可以使用属性和过程的 专用 实现,但这些成员只能通过将实现类的实例强制转换为声明为接口类型的变量来访问。
示例 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