VBA:有抽象类之类的东西吗? [英] VBA: is there something like Abstract Class?

查看:25
本文介绍了VBA:有抽象类之类的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个接口来确保一些类似的类实现了一些强制方法(子/函数).

I'm using an interface to ensure some similar classes implements some mandatory methods (subs/functions).

示例:

  • 接口 I1 声明了 M1 和 M2 方法
  • C1 和 C2 实现了 I1,并且有自己的 M1 和 M2 版本.

C1 和 C2 也需要完全相同的方法,例如方法 SM1 和 SM2.

C1 and C2 also need methods that are exactly the same, for example methods SM1 and SM2.

为了避免重复 SM1 和 SM2,我想定义一个抽象类 AC:

To avoid repeating SM1 and SM2 I'd like to define an abstract class AC:

  • 实施 I1
  • 定义 SM1 和 SM2.

由 C1 和 C2 扩展

which would be extended by C1 and C2

这个解决方案在 Java 中确实可行,但我没有找到任何在 VBA 中执行相同操作的文档.(VB.Net 似乎允许使用关键字 MustInherit 的抽象类.)

This solution is indeed possible in Java, but I don't find any documentation for doing the same in VBA. (VB.Net seems to allow abstract classes using keyword MustInherit.)

是否可以在 VBA 中确认是否可行?

推荐答案

你可以在 VBA 中实现 semi-decorator :-) 模式.假设我们有一个基类和两个子类.如果您在子类中使用Implements 关键字,您将保证子类与基类具有相同的接口,同时在每个子类中声明基类的私有实例并将子类的调用重定向到基类.

You could implement semi-decorator :-) pattern in VBA. Lets say we have a base class and two child classes. If you use Implements keyword in child classes you will garatee that child classes has the same interface as the base class and at the same time declare a private instance of base class in each child class and redirect calls from children to base class.

注意:这里的基类是普通类,你仍然可以创建它的实例(所以它不是真正的抽象类).

Note: base class here is normal class, you can still create instances of it (so it is not real abstract class).

示例:

' Standard module

Sub main()
    Dim a As New ChildA
    Dim b As New ChildB

    a.State = 2
    b.State = 5

    Debug.Print TypeOf a Is Base
    Debug.Print TypeOf b Is Base

    TestPolymorphic a
    TestPolymorphic b
End Sub

Private Sub TestPolymorphic(ByRef obj As Base)
    obj.Polymorphic
End Sub

' -----------------------------------------------

' Base class module

Private m_state As Byte

Public Event StateChanged(oldState As Byte, newState As Byte)

Public Property Get State() As Byte
    State = m_state
End Property

Public Property Let State(ByVal newState As Byte)
    Dim oldState As Byte
    oldState = m_state
    m_state = newState
    RaiseEvent StateChanged(oldState, newState)
End Property

Sub Polymorphic()
    Err.Raise 123, , "Implement in child class"
End Sub

Private Sub Class_Initialize()
    m_state = 1
End Sub

' -----------------------------------------------

' ChildA class module

Implements Base
Private WithEvents m_base As Base

Private Sub Class_Initialize()
    Set m_base = New Base
End Sub

Public Property Get State() As Byte
    State = Base_State
End Property

Public Property Let State(ByVal newState As Byte)
    Base_State = newState
End Property

Public Sub Polymorphic()
    Base_Polymorphic
End Sub

Private Property Get Base_State() As Byte
    Base_State = m_base.State
End Property

Private Property Let Base_State(ByVal newState As Byte)
    m_base.State = newState
End Property

Private Sub Base_Polymorphic()
    Debug.Print "In Child A ..."
End Sub

Private Sub m_base_StateChanged(oldState As Byte, newState As Byte)
    Debug.Print "State of 'Child A' instance has changed from " & oldState & " to " & newState
End Sub

Output:
' State of 'Child A' instance has changed from 1 to 2
' State of 'Child B' instance has changed from 1 to 5
' True
' True
' In Child A ...
' In Child B ...

这篇关于VBA:有抽象类之类的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆