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

时间:2019-06-05 13:56:03

标签: vba

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

这是IXMLSerializable.cls文件的内容。

runTimer()

3 个答案:

答案 0 :(得分:3)

请注意,VBA支持接口,就像C#/VB.NET一样(几乎)一样。接口是在VBA中提供继承机制的 only 方法。

  

By convention接口的名称以大写字母I开头。

这是示例接口声明,其中声明对象必须定义name属性

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

Public Property Get Name() As String
End Property

如您所见,不需要实施。

现在创建一个对象,该对象使用该接口来通告它包含name属性。当然,关键是有多个类使用一个接口。

[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

为方便起见,在用户界面中添加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

答案 1 :(得分:2)

I代表接口,如Microsoft Official Documentation中所指定:

  

IXMLDOMElement成员。

     

下表显示了属性,方法和事件。

     

在C ++中,此接口继承自 IXMLDOMNode

这是一个非常普遍的约定,通过这样做,您立即知道它代表Interface,而无需查看代码。

希望这会有所帮助。

答案 2 :(得分:0)

I代表界面。据说VBA和VB 6.0以前的Visual Basic方言是面向对象的,但是对它的支持却很差。例如,没有类继承。不过,您可以在VBA / VB6中声明和实现接口;但是,没有Interface关键字,因为有Class关键字。相反,您只需要声明一个包含空的Subs,Functions和Properties的类。

示例。在名为IComparable的类中,声明一个Function CompareTo

Public Function CompareTo(ByVal other As Object) As Long
    'Must return -1, 0 or +1, if current object is less than, equal to or greater than obj.
    'Must be empty here.
End Function

现在,您可以声明实现此接口的类。例如。一个名为clsDocument的类:

Implements IComparer

public Name as String

Private Function IComparable_CompareTo(other As Variant) As Long
    IComparable_CompareTo = StrComp(Name, other.Name, vbTextCompare)
End Function

现在,这使您可以创建搜索和排序算法,可以将它们应用于实现此方法的不同类类型。名为Document

的类的示例
Option Explicit

Implements IComparable

Public Name As String
Public FileDate As Date

Public Function IComparable_CompareTo(ByVal other As Object) As Long
    Dim doc As Document, comp As Long

    Set doc = other
    comp = StrComp(Me.Name, doc.Name, vbTextCompare)
    If comp = 0 Then
        If Me.FileDate < doc.FileDate Then
            IComparable_CompareTo = -1
        ElseIf Me.FileDate > doc.FileDate Then
            IComparable_CompareTo = + 1
        Else
            IComparable_CompareTo = 0
        End If
    Else
        IComparable_CompareTo = comp
    End If
End Function

这里是VBA的QuickSort的示例。假定您将其传递给IComparables数组:

Public Sub QuickSort(ByRef a() As IComparable)
'Sorts a unidimensional array of IComparable's in ascending order very quickly.
    Dim l As Long, u As Long

    l = LBound(a)
    u = UBound(a)
    If u > l Then
        QS a, l, u
    End If
End Sub

Private Sub QS(ByRef a() As IComparable, ByVal Low As Long, ByVal HI As Long)
'Very fast sort: n Log n comparisons
    Dim i As Long, j As Long, w As IComparable, x As IComparable

    i = Low:   j = HI
    Set x = a((Low + HI) \ 2)
    Do
        While a(i).CompareTo(x) = -1:   i = i + 1:   Wend
        While a(j).CompareTo(x) = 1:   j = j - 1:   Wend
        If i <= j Then
            Set w = a(i):   Set a(i) = a(j):   Set a(j) = w
            i = i + 1:   j = j - 1
        End If
    Loop Until i > j
    If Low < j Then QS a, Low, j
    If HI > i Then QS a, i, HI
End Sub