为什么有些班级的名字前面带有“ I”?

Jos*_*Vey 5 vba

我正在使用Visual Basic 98中的一些旧代码,并且有几个类的名称前面带有“ I”。但是,大多数类都没有此名称。

这是IXMLSerializable.cls文件的内容。

' Serialization XML:
'       Create and receive complete split data through XML node
Public Property Let SerializationXML(ByVal p_sXML As String)
End Property
Public Property Get SerializationXML() As String
End Property

Public Property Get SerializationXMLElement() As IXMLDOMElement
End Property
Run Code Online (Sandbox Code Playgroud)

ja7*_*a72 4

请注意,VBA支持接口,就像C#/VB.NET(几乎)一样。接口是VBA.

按照惯例,接口的名称以大写字母开头I

这是一个示例接口声明,声明对象必须定义名称属性

[File: IHasName.cls, Instancing: PublicNotCreatable] 
Option Explicit

Public Property Get Name() As String
End Property
Run Code Online (Sandbox Code Playgroud)

如您所见,无需实施。

现在创建一个对象,该对象使用该接口来通告它包含名称属性。当然,重点是有多个类使用同一个接口。

[File: Person.cls, Instancing: Private]
Option Explicit
Implements IHasName

Private m_name As String

Private Sub Class_Initialize()
    m_name = "<Empty>"
End Sub

' Local property
Public Property Get Name() as String
    Name = m_name
End Property

Public Property Let Name(ByVal x As String)
    m_name = x
End Property

' This is the interface implementation that relies on local the property `Name` 
Private Property Get IHasName_Name() As String
    IHasName_Name = Name
End Property
Run Code Online (Sandbox Code Playgroud)

为了方便用户界面,一旦包含该Implements语句,您可以从顶部选择界面属性

SCR

要使用上面的代码,请使用以下测试,该测试调用一个可以接受任何实现IHasName.

[File: Module1.bas]
Option Explicit

Public Sub TestInterface()

    Dim target As New Person
    target.Name = "John"

    GenReport target
    ' This prints the name "John".
End Sub

Public Function GenReport(ByVal obj As IHasName)
    Debug.Print obj.Name
End Function
Run Code Online (Sandbox Code Playgroud)