VBA: 추상 수업 같은 것이 있나요?
인터페이스를 사용하여 유사한 클래스에서 필수 메서드(하위/함수)를 구현하고 있습니다.
예:
- 인터페이스 I1이 M1 및 M2 메서드를 선언합니다.
- C1 및 C2는 I1을 구현하고 M1 및 M2용 자체 버전을 가지고 있습니다.
또한 C1과 C2는 정확히 동일한 방법(예: 방법 SM1과 SM2)이 필요합니다.
SM1과 SM2가 반복되지 않도록 추상 클래스 AC를 정의합니다.
- I1 구현
- SM1 및 SM2를 정의합니다.
그것은 C1과 C2에 의해 연장될 것입니다.
이 솔루션은 Java에서 실제로 가능하지만 VBA(VB)에서 동일한 작업을 수행하는 데 대한 설명서를 찾을 수 없습니다.Net에서 MustInherit 키워드를 사용하여 추상 클래스를 허용하는 것 같습니다.)
VBA에서 가능한지 아닌지 확인할 수 있습니까?
VBA에는 상속이 없습니다.
인터페이스를 정의할 수 있으며 다음을 사용하여 클래스에서 구현할 수 있습니다.Implements
키워드그러나 기본 클래스에서 공유 기능을 미리 구현하려면 복사 붙여넣기 방식을 사용해야 합니다.
관련 판독값:
Excel VBA에서 구현체를 사용하는 방법
VBA에서 클래스 객체 모듈 간 비교 방법을 VB와 유사하게 사용하는 방법NET?
당신은 VBA에서 세미 데코레이터 :-) 패턴을 구현할 수 있습니다.기본 클래스와 두 개의 어린이 클래스가 있다고 가정해 보겠습니다.자식 클래스에서 Implements 키워드를 사용하면 자식 클래스가 기본 클래스와 동일한 인터페이스를 가지고 있음을 확인하고 동시에 각 자식 클래스에서 기본 클래스의 개인 인스턴스를 선언하고 자식 클래스에서 기본 클래스로 호출을 리디렉션합니다.
참고: 여기서 기본 클래스는 일반 클래스이므로 해당 클래스의 인스턴스를 계속 만들 수 있습니다(따라서 실제 추상 클래스가 아님).
예:
' 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의 추상 수업을 위한 나의 솔루션: View
'-------------------------
' Standard module
Sub Main()
Dim objC1 As C1
Dim objC2 As C2
Dim objCollection As New Collection
Set objC1 = New C1
Set objC2 = New C2
With objC1
.getInterface.M1 "Hello C1!"
temp1 = .getInterface.M2(objCollection)
.getSuperClass.SM1 "Hi C1!!!"
temp3 = .getSuperClass.SM2(objCollection)
End With
Debug.Print vbCrLf
With objC2
.getInterface.M1 "Hello C2!"
temp1 = .getInterface.M2(objCollection)
.getSuperClass.SM1 "Hi C2!!!"
temp3 = .getSuperClass.SM2(objCollection)
End With
End Sub
' -----------------------------------------------
' IAbstracat class module
Sub SM1(strString As String): End Sub
Function SM2(varItem As Variant) As String: End Function
' -----------------------------------------------
' Abstracat class module
Implements IAbstract
'Each class must implement these methods, in a particular way
Sub M1(strString As String): End Sub
Function M2(varItem As Variant) As String: End Function
'The sub classes will extend SM1 and SM2
Private Sub IAbstract_SM1(strString As String)
Debug.Print "Sub IAbstract_SM1: " & strString
End Sub
'The sub classes will extend SM1 and SM2
Private Function IAbstract_SM2(varItem As Variant) As String
Dim strMyString As String
strMyString = "Function IAbstract_SM2 => ObjPtr(varItem): " & ObjPtr(varItem)
Debug.Print strMyString
IAbstract_SM2 = strMyString
End Function
' -----------------------------------------------
' C1 class module
Implements Abstract
Private Type TC1
objConcretSuperClass As Abstract
objInterfaceSuperClass As IAbstract
objInterfaceSubClass As Abstract
End Type
Private this As TC1
'if you do not need to initialize anything, then this is it:
Private Sub Class_Initialize()
With this
'creating an instance of class' Abstract'
Set .objConcretSuperClass = New Abstract
'Referencing the' Abstract 'interface, where are the extended methods
Set .objInterfaceSuperClass = .objConcretSuperClass
'Creating a refence for the C1 interface, which is the class' Abstract'
'Here we have the particular implementations of M1 and M2
Set .objInterfaceSubClass = Me
End With
End Sub
'With this we can do:
' set objC1 = New C1
' objC1.getInterface.Abstract_M1
' objC1.getInterface.Abstract_M2
Property Get getInterface() As Abstract
Set getInterface = this.objInterfaceSubClass
End Property
'With this we can call the methods defined in' Abstract ': SM1 and SM2
' set objC1 = New C1
' objC1.getSuperClass.SM1 "hello!"
' temp = objC1.getSuperClass.SM2 (New Collection)
Property Get getSuperClass() As IAbstract
Set getSuperClass = this.objInterfaceSuperClass
End Property
'Here we have the particular implementations of M1 and M2
Private Sub Abstract_M1(strString As String)
Debug.Print "Class C1 => Sub Abstract_M1: " & strString
End Sub
Private Function Abstract_M2(varItem As Variant) As String
Debug.Print "Class C1 => Function Abstract_M2: " & ObjPtr(varItem)
End Function
' -----------------------------------------------
' C2 class module
Implements Abstract
Private Type TC2
objConcretSuperClass As Abstract
objInterfaceSuperClass As IAbstract
objInterfaceSubClass As Abstract
End Type
Private this As TC2
'if you do not need to initialize anything, then this is it:
Private Sub Class_Initialize()
With this
'creating an instance of class' Abstract'
Set .objConcretSuperClass = New Abstract
'Referencing the' Abstract 'interface, where are the extended methods
Set .objInterfaceSuperClass = .objConcretSuperClass
'Creating a refence for the C1 interface, which is the class' Abstract'
'Here we have the particular implementations of M1 and M2
Set .objInterfaceSubClass = Me
End With
End Sub
'With this we can do:
' set objC2 = New C2
' objC2.getInterface.Abstract_M1
' objC2.getInterface.Abstract_M2
Property Get getInterface() As Abstract
Set getInterface = this.objInterfaceSubClass
End Property
'With this we can call the methods defined in' Abstract ': SM1 and SM2
' set objC1 = New C1
' objC1.getSuperClass.SM1 "hello!"
' temp = objC1.getSuperClass.SM2 (New Collection)
Property Get getSuperClass() As IAbstract
Set getSuperClass = this.objInterfaceSuperClass
End Property
'Here we have the particular implementations of M1 and M2
Private Sub Abstract_M1(strString As String)
Debug.Print "Class C2 => Sub Abstract_M1: " & strString
End Sub
Private Function Abstract_M2(varItem As Variant) As String
Debug.Print "Class C2 => Function Abstract_M2: " & ObjPtr(varItem)
End Function
즉시 확인 창(CTRL + G):
Class C1 => Sub Abstract_M1: Hello C1!
Class C1 => Function Abstract_M2: 550324728
Sub IAbstract_SM1: Hi C1!!!
Function IAbstract_SM2 => ObjPtr(varItem): 550324728
Class C2 => Sub Abstract_M1: Hello C2!
Class C2 => Function Abstract_M2: 550324728
Sub IAbstract_SM1: Hi C2!!!
Function IAbstract_SM2 => ObjPtr(varItem): 550324728
언급URL : https://stackoverflow.com/questions/21220317/vba-is-there-something-like-abstract-class
'sourcecode' 카테고리의 다른 글
봄 "URL이 정상화되지 않아 요청이 거부되었습니다."어떤 URL이 사용되었는지 어떻게 알 수 있습니까? (0) | 2023.08.11 |
---|---|
php에서 데이터베이스 데이터를 비교하는 방법 (0) | 2023.08.11 |
공백이 있는 문자열을 PowerShell로 전달하려면 어떻게 해야 합니까? (0) | 2023.08.11 |
Asp에서 로그인한 사용자의 사용자 ID를 가져옵니다.넷 MVC 5 (0) | 2023.08.11 |
데이터베이스 스키마가 null 가능한 열에 대해 동기화되지 않았습니다. (0) | 2023.08.11 |