Nice programing

Excel VBA에서 구현을 사용하는 방법

nicepro 2020. 12. 7. 20:40
반응형

Excel VBA에서 구현을 사용하는 방법


나는 엔지니어링 프로젝트를 위해 몇 가지 모양을 구현하고 일반화 된 프로그램을 가질 수 있도록 몇 가지 일반적인 기능을 위해 추상화하려고합니다.

난 할 노력하고있어 것은라는 인터페이스를 cShape하고있다 cRectanglecCircle구현cShape

내 코드는 다음과 같습니다.

cShape 상호 작용

Option Explicit

Public Function getArea()
End Function

Public Function getInertiaX()
End Function

Public Function getInertiaY()
End Function

Public Function toString()
End Function

cRectangle 수업

Option Explicit
Implements cShape

Public myLength As Double ''going to treat length as d
Public myWidth As Double ''going to treat width as b

Public Function getArea()
    getArea = myLength * myWidth
End Function

Public Function getInertiaX()
    getInertiaX = (myWidth) * (myLength ^ 3)
End Function

Public Function getInertiaY()
    getInertiaY = (myLength) * (myWidth ^ 3)
End Function

Public Function toString()
    toString = "This is a " & myWidth & " by " & myLength & " rectangle."
End Function

cCircle 수업

Option Explicit
Implements cShape

Public myRadius As Double

Public Function getDiameter()
    getDiameter = 2 * myRadius
End Function

Public Function getArea()
    getArea = Application.WorksheetFunction.Pi() * (myRadius ^ 2)
End Function

''Inertia around the X axis
Public Function getInertiaX()
    getInertiaX = Application.WorksheetFunction.Pi() / 4 * (myRadius ^ 4)
End Function

''Inertia around the Y axis
''Ix = Iy in a circle, technically should use same function
Public Function getInertiaY()
    getInertiaY = Application.WorksheetFunction.Pi() / 4 * (myRadius ^ 4)
End Function

Public Function toString()
    toString = "This is a radius " & myRadius & " circle."
End Function

문제는 테스트 케이스를 실행할 때마다 다음과 같은 오류가 발생한다는 것입니다.

컴파일 오류 :

개체 모듈은 '~'인터페이스에 '~'를 구현해야합니다.


이것은 난해한 OOP 개념이며 사용자 지정 셰이프 컬렉션을 사용하기 위해 수행하고 이해해야하는 것이 조금 더 있습니다.

먼저 this answerVBA의 클래스 및 인터페이스에 대한 일반적인 이해를 살펴볼 수 있습니다.


아래 지침을 따르십시오.

먼저 메모장을 열고 아래 코드를 복사하여 붙여 넣으십시오.

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1
END
Attribute VB_Name = "ShapesCollection"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Dim myCustomCollection As Collection

Private Sub Class_Initialize()
    Set myCustomCollection = New Collection
End Sub

Public Sub Class_Terminate()
    Set myCustomCollection = Nothing
End Sub

Public Sub Add(ByVal Item As Object)
    myCustomCollection.Add Item
End Sub

Public Sub AddShapes(ParamArray arr() As Variant)
    Dim v As Variant
    For Each v In arr
        myCustomCollection.Add v
    Next
End Sub

Public Sub Remove(index As Variant)
    myCustomCollection.Remove (index)
End Sub

Public Property Get Item(index As Long) As cShape
    Set Item = myCustomCollection.Item(index)
End Property

Public Property Get Count() As Long
    Count = myCustomCollection.Count
End Property

Public Property Get NewEnum() As IUnknown
    Attribute NewEnum.VB_UserMemId = -4
    Attribute NewEnum.VB_MemberFlags = "40"
    Set NewEnum = myCustomCollection.[_NewEnum]
End Property

파일을 ShapesCollection.cls데스크탑에 다른 이름 으로 저장하십시오 .

확장이 아닌 확장으로 저장하고 있는지 확인하십시오.*.clsShapesCollection.cls.txt

이제 Excel 파일을 열고 VBE ALT+ 로 이동 F11하여 Project Explorer. Import File드롭 다운 메뉴에서 선택 하고 파일로 이동합니다.

여기에 이미지 설명 입력

주의 : .clsVBEditor는 속성 사용을 허용하지 않기 때문에 먼저 파일에 코드를 저장 한 다음 가져와야했습니다. 속성을 사용하면 반복에서 기본 멤버를 지정하고 사용자 정의 컬렉션 클래스에서 for each 루프를 사용할 수 있습니다.

더보기:

이제 3 개의 클래스 모듈을 삽입합니다. 그에 따라 이름을 변경하고 코드를 복사하여 붙여 넣습니다.

cShape 이것은 귀하의 인터페이스입니다

Public Function GetArea() As Double
End Function

Public Function GetInertiaX() As Double
End Function

Public Function GetInertiaY() As Double
End Function

Public Function ToString() As String
End Function

cCircle

Option Explicit

Implements cShape

Public Radius As Double

Public Function GetDiameter() As Double
    GetDiameter = 2 * Radius
End Function

Public Function GetArea() As Double
    GetArea = Application.WorksheetFunction.Pi() * (Radius ^ 2)
End Function

''Inertia around the X axis
Public Function GetInertiaX() As Double
    GetInertiaX = Application.WorksheetFunction.Pi() / 4 * (Radius ^ 4)
End Function

''Inertia around the Y axis
''Ix = Iy in a circle, technically should use same function
Public Function GetInertiaY() As Double
    GetInertiaY = Application.WorksheetFunction.Pi() / 4 * (Radius ^ 4)
End Function

Public Function ToString() As String
    ToString = "This is a radius " & Radius & " circle."
End Function

'interface functions
Private Function cShape_getArea() As Double
    cShape_getArea = GetArea
End Function

Private Function cShape_getInertiaX() As Double
    cShape_getInertiaX = GetInertiaX
End Function

Private Function cShape_getInertiaY() As Double
    cShape_getInertiaY = GetInertiaY
End Function

Private Function cShape_toString() As String
    cShape_toString = ToString
End Function

cRectangle

Option Explicit

Implements cShape

Public Length As Double ''going to treat length as d
Public Width As Double ''going to treat width as b

Public Function GetArea() As Double
    GetArea = Length * Width
End Function

Public Function GetInertiaX() As Double
    GetInertiaX = (Width) * (Length ^ 3)
End Function

Public Function GetInertiaY() As Double
    GetInertiaY = (Length) * (Width ^ 3)
End Function

Public Function ToString() As String
    ToString = "This is a " & Width & " by " & Length & " rectangle."
End Function

' interface properties
Private Function cShape_getArea() As Double
    cShape_getArea = GetArea
End Function

Private Function cShape_getInertiaX() As Double
    cShape_getInertiaX = GetInertiaX
End Function

Private Function cShape_getInertiaY() As Double
    cShape_getInertiaY = GetInertiaY
End Function

Private Function cShape_toString() As String
    cShape_toString = ToString
End Function

지금 Insert표준 이 필요 Module하고 아래 코드를 복사하여 붙여 넣으십시오.

모듈 1

Option Explicit

Sub Main()

    Dim shapes As ShapesCollection
    Set shapes = New ShapesCollection

    AddShapesTo shapes

    Dim iShape As cShape
    For Each iShape In shapes
        'If TypeOf iShape Is cCircle Then
            Debug.Print iShape.ToString, "Area: " & iShape.GetArea, "InertiaX: " & iShape.GetInertiaX, "InertiaY:" & iShape.GetInertiaY
        'End If
    Next

End Sub


Private Sub AddShapesTo(ByRef shapes As ShapesCollection)

    Dim c1 As New cCircle
    c1.Radius = 10.5

    Dim c2 As New cCircle
    c2.Radius = 78.265

    Dim r1 As New cRectangle
    r1.Length = 80.87
    r1.Width = 20.6

    Dim r2 As New cRectangle
    r2.Length = 12.14
    r2.Width = 40.74

    shapes.AddShapes c1, c2, r1, r2
End Sub

MainSub를 실행하고 + 에서 결과를 확인하십시오.Immediate Window CTRLG

여기에 이미지 설명 입력


의견 및 설명 :

당신의에서 ShapesCollection클래스 모듈 컬렉션에 항목을 추가하기위한 2 잠수정이있다.

첫 번째 메서드는 Public Sub Add(ByVal Item As Object)단순히 클래스 인스턴스를 가져와 컬렉션에 추가합니다. Module1이런 식으로 사용할 수 있습니다.

Dim c1 As New cCircle
shapes.Add c1

Public Sub AddShapes(ParamArray arr() As Variant)사용하면 Sub ,와 동일한 방식으로 쉼표로 구분 하여 동시에 여러 개체를 추가 할 수 있습니다 AddShapes().

각 개체를 개별적으로 추가하는 것보다 훨씬 더 나은 디자인이지만 어떤 개체를 사용할지는 귀하에게 달려 있습니다.

루프에서 몇 가지 코드를 주석 처리했는지 확인하십시오.

Dim iShape As cShape
For Each iShape In shapes
    'If TypeOf iShape Is cCircle Then
        Debug.Print iShape.ToString, "Area: " & iShape.GetArea, "InertiaX: " & iShape.GetInertiaX, "InertiaY:" & iShape.GetInertiaY
    'End If
Next

'If'End If에서 주석을 제거 하면 cCircle개체 만 인쇄 할 수 있습니다. 이것은 VBA에서 델리게이트를 사용할 수 있다면 정말 유용 할 것이지만 그렇게 할 수 없기 때문에 한 가지 유형의 개체 만 인쇄하는 다른 방법을 보여 드렸습니다. If필요에 맞게 명령문을 수정 하거나 단순히 모든 개체를 인쇄 할 수 있습니다. 다시 말하지만, 데이터를 처리하는 방법은 사용자에게 달려 있습니다. :)


여기에 구현 / 인터페이스가 무엇인지 궁금해하는 사람들이 여기에 도착한 경우 주어진 답변에 대한 이론적 및 실제적 기여가 있습니다.

아시다시피 VBA는 상속을 지원하지 않으므로 인터페이스를 거의 맹목적으로 사용하여 서로 다른 클래스에서 공통 속성 / 동작을 구현할 수 있습니다.
그래도 나중에 중요한 이유를 알기 위해 둘 사이의 개념적 차이가 무엇인지 설명하는 것이 유용하다고 생각합니다.

  • 상속 : is-a 관계를 정의합니다 (사각형 is-a 모양).
  • 인터페이스 :해야 할 일 관계를 정의합니다 (일반적인 예는 drawable드로어 블 객체가 메서드를 구현해야한다고 규정하는 인터페이스입니다 draw). 이는 다른 루트 클래스에서 생성 된 클래스가 공통 동작을 구현할 수 있음을 의미합니다.

상속은 기본 클래스 (일부 물리적 또는 개념적 원형)가 확장 되는 반면 인터페이스 는 특정 동작 을 정의하는 속성 / 메소드 집합을 구현 합니다 . 따라서 다른 모든 셰이프가 상속되는 기본 클래스이며 모든 셰이프를 드로어 블로 만드는 인터페이스를 구현할 수 있습니다 . 이 인터페이스는 모든 Shape가 어떻게 / 어디에 모양을 그려야 하는지를 지정하는 메서드 가 있음을 보장하는 계약입니다 . 원은 사각형과 다르게 그려 지거나 그려지지 않을 수 있습니다.
Shapedrawabledraw

클래스 IDrawable :

'IDrawable interface, defining what methods drawable objects have access to
Public Function draw()
End Function

VBA는 상속을 지원하지 않기 때문에, 우리는 추상적 인 Shape베이스 클래스를 생성하는 대신 일반 모양 (사각형, 원형 ​​등)에 의해 구현되는 특정 속성 / 동작을 보장하는 인터페이스 IShape를 생성하도록 자동으로 선택해야합니다. 확장 할 수 있습니다.

클래스 IShape :

'Get the area of a shape
Public Function getArea() As Double
End Function

문제가되는 부분은 모든 Shape를 드로어 블로 만들고 싶을 때입니다.
불행히도 IShape는 VBA의 기본 클래스가 아닌 인터페이스이기 때문에 기본 클래스에서 드로어 블 인터페이스를 구현할 수 없습니다. VBA는 한 인터페이스가 다른 인터페이스를 구현하도록 허용하지 않는 것 같습니다. 이것을 테스트 한 후 컴파일러가 원하는 동작을 제공하지 않는 것 같습니다. 즉, IShape 내에서 IDrawable을 구현할 수 없으며 IShape의 인스턴스가 이로 인해 IDrawable 메서드를 강제로 구현할 것으로 예상합니다.
IShape 인터페이스를 구현하는 모든 일반 셰이프 클래스에이 인터페이스를 구현해야하며 다행히 VBA를 사용하면 여러 인터페이스를 구현할 수 있습니다.

클래스 cSquare :

Option Explicit

Implements iShape
Implements IDrawable

Private pWidth          As Double
Private pHeight         As Double
Private pPositionX      As Double
Private pPositionY      As Double

Public Function iShape_getArea() As Double
    getArea = pWidth * pHeight
End Function

Public Function IDrawable_draw()
    debug.print "Draw square method"
End Function

'Getters and setters

이제 다음 부분은 인터페이스의 일반적인 사용 / 혜택이 작용하는 부분입니다.

새 사각형을 반환하는 팩토리를 작성하여 코드를 시작하겠습니다. (이것은 생성자에 직접 인수를 보낼 수없는 문제에 대한 해결 방법 일뿐입니다) :

모듈 mFactory :

Public Function createSquare(width, height, x, y) As cSquare

    Dim square As New cSquare

    square.width = width
    square.height = height
    square.positionX = x
    square.positionY = y

    Set createSquare = square

End Function

우리의 메인 코드는 팩토리를 사용하여 새 Square를 만듭니다.

Dim square          As cSquare

Set square = mFactory.createSquare(5, 5, 0, 0)

원하는대로 사용할 수있는 메서드를 살펴보면 cSquare 클래스에 정의 된 모든 메서드에 논리적으로 액세스 할 수 있음을 알 수 있습니다.

여기에 이미지 설명 입력

이것이 왜 관련이 있는지 나중에 살펴 보겠습니다.

이제 드로어 블 개체 모음을 실제로 만들고 싶다면 어떤 일이 일어날 지 궁금 할 것입니다. 앱에는 모양이 아니지만 아직 그릴 수있는 개체가 포함될 수 있습니다. 이론적으로는 그릴 수있는 IComputer 인터페이스 (클립 아트 등)를 사용하는 것을 방해하는 것은 없습니다.
드로어 블 개체 모음을 갖고 싶은 이유는 앱 수명주기의 특정 지점에서 루프로 렌더링 할 수 있기 때문입니다.

이 경우 컬렉션을 래핑하는 데코레이터 클래스를 작성합니다 (이유를 살펴 보겠습니다). collDrawables 클래스 :

Option Explicit

Private pSize As Integer
Private pDrawables As Collection

'constructor
Public Sub class_initialize()
    Set pDrawables = New Collection
End Sub

'Adds a drawable to the collection
Public Sub add(cDrawable As IDrawable)
    pDrawables.add cDrawable

    'Increase collection size
    pSize = pSize + 1

End Sub

데코레이터를 사용하면 네이티브 vba 컬렉션이 제공하지 않는 몇 가지 편리한 메서드를 추가 할 수 있지만 여기서 실제 요점은 컬렉션이 드로어 블 개체 만 허용한다는 것입니다 (IDrawable 인터페이스 구현). 드로어 블이 아닌 객체를 추가하려고하면 유형 불일치가 발생합니다 (드로어 블 객체 만 허용됩니다!).

그래서 우리는 그것들을 렌더링하기 위해 드로어 블 객체의 컬렉션을 반복 할 수 있습니다. 그릴 수없는 개체를 컬렉션에 허용하면 버그가 발생합니다. 렌더링 루프는 다음과 같습니다.

명시 적 옵션

Public Sub app()

    Dim obj             As IDrawable
    Dim square_1        As IDrawable
    Dim square_2        As IDrawable
    Dim computer        As IDrawable
    Dim person          as cPerson 'Not drawable(!) 
    Dim collRender      As New collDrawables

    Set square_1 = mFactory.createSquare(5, 5, 0, 0)
    Set square_2 = mFactory.createSquare(10, 5, 0, 0)
    Set computer = mFactory.createComputer(20, 20)

    collRender.add square_1
    collRender.add square_2
    collRender.add computer

    'This is the loop, we are sure that all objects are drawable! 
    For Each obj In collRender.getDrawables
        obj.draw
    Next obj

End Sub

위의 코드는 많은 투명성을 추가합니다. 우리는 객체를 IDrawable로 선언했습니다. 이는 컬렉션 내의 모든 객체에서 그리기 메서드를 사용할 수 있기 때문에 루프가 실패하지 않을 것임을 투명하게 만듭니다.
Person을 컬렉션에 추가하려고하면이 Person 클래스가 드로어 블 인터페이스를 구현하지 않으면 유형 불일치가 발생합니다.

그러나 아마도 객체를 인터페이스로 선언하는 것이 중요한 이유는 아마도 우리가 이전에 본 것처럼 개별 클래스에 정의 된 공용 메서드가 아니라 interface에 정의 된 메서드 만 노출하기를 원하기 때문일 것입니다. .

Dim square_1        As IDrawable 

여기에 이미지 설명 입력

square_1에 draw메서드 가 있음을 확신 할 수있을 뿐만 아니라 IDrawable에서 정의한 메서드 노출되도록합니다.
사각형의 경우이 기능의 이점이 즉시 명확하지 않을 수 있지만 훨씬 더 명확한 Java 컬렉션 프레임 워크의 비유를 살펴 보겠습니다.

IList여러 유형의 목록에 적용 할 수있는 메서드 집합을 정의 하는 일반 인터페이스가 있다고 상상해보십시오 . 각 유형의 목록은 IList 인터페이스를 구현하고 자체 동작을 정의하고 가능한 더 많은 자체 메서드를 맨 위에 추가하는 특정 클래스입니다.

목록을 다음과 같이 선언합니다.

dim myList as IList 'Declare as the interface! 

set myList = new ArrayList 'Implements the interface of IList only, ArrayList allows random (index-based) access 

위의 코드에서 목록을 IList로 선언하면 ArrayList 관련 메서드를 사용하지 않고 인터페이스에서 지정한 메서드 만 사용할 수 있습니다. 다음과 같이 목록을 선언했다고 상상해보십시오.

dim myList as ArrayList 'We don't want this

ArrayList 클래스에 특별히 정의 된 공용 메서드에 액세스 할 수 있습니다. 때로는 이것이 바람직 할 수도 있지만 종종 클래스 별 공용 메서드에 의해 정의되지 않고 내부 클래스 동작의 이점을 얻고 자합니다.
코드에서이 ArrayList를 50 번 더 사용하면 이점이 분명 해지고 갑자기 LinkedList (이 유형의 List와 관련된 특정 내부 동작을 허용 함)를 사용하는 것이 더 낫다는 것을 알게됩니다.

인터페이스를 준수하면 다음 행을 변경할 수 있습니다.

set myList = new ArrayList

에:

set myList = new LinkedList 

인터페이스가 계약이 이행되었는지 확인하기 때문에 다른 코드는 손상되지 않습니다. IList에 정의 된 공용 메서드 만 사용되므로 시간이 지남에 따라 다른 유형의 목록을 바꿀 수 있습니다.

마지막으로 (VBA에서 덜 알려진 동작) 인터페이스에 기본 구현을 제공 할 수 있습니다.

다음과 같은 방법으로 인터페이스를 정의 할 수 있습니다.

IDrawable :

Public Function draw()
    Debug.Print "Draw interface method"
End Function

그리고 draw 메서드도 구현하는 클래스 :

cSquare :

implements IDrawable 
Public Function draw()
    Debug.Print "Draw square method" 
End Function

다음과 같은 방법으로 구현간에 전환 할 수 있습니다.

Dim square_1        As IDrawable

Set square_1 = New IDrawable
square_1.draw 'Draw interface method
Set square_1 = New cSquare
square_1.draw 'Draw square method    

변수를 cSquare로 선언하면 불가능합니다.
이것이 유용 할 때 좋은 예를 즉시 생각할 수는 없지만 테스트 해 보면 기술적으로 가능합니다.


VBA 및 "Implements"문에 대해 문서화되지 않은 두 가지 추가 사항이 있습니다.

  1. VBA는 파생 클래스의 상속 된 인터페이스의 메서드 이름에서 undescore 문자 '_'를 지원하지 않습니다. Fe는 cShape.get_area (Excel 2007에서 테스트 됨)와 같은 메서드로 코드를 컴파일하지 않습니다. VBA는 파생 클래스에 대해 위의 컴파일 오류를 출력합니다.

  2. 파생 클래스가 인터페이스에서와 같이 명명 된 자체 메서드를 구현하지 않는 경우 VBA는 코드를 성공적으로 컴파일하지만 메서드는 파생 클래스 유형의 변수를 통해 비활성화됩니다.


사용되는 클래스에서 모든 인터페이스 메서드를 구현해야합니다.

cCircle 클래스

Option Explicit
Implements cShape

Public myRadius As Double

Public Function getDiameter()
    getDiameter = 2 * myRadius
End Function

Public Function getArea()
    getArea = Application.WorksheetFunction.Pi() * (myRadius ^ 2)
End Function

''Inertia around the X axis
Public Function getInertiaX()
    getInertiaX = Application.WorksheetFunction.Pi() / 4 * (myRadius ^ 4)
End Function

''Inertia around the Y axis
''Ix = Iy in a circle, technically should use same function
Public Function getIntertiaY()
    getIntertiaY = Application.WorksheetFunction.Pi() / 4 * (myRadius ^ 4)
End Function

Public Function toString()
    toString = "This is a radius " & myRadius & " circle."
End Function

Private Function cShape_getArea() As Variant

End Function

Private Function cShape_getInertiaX() As Variant

End Function

Private Function cShape_getIntertiaY() As Variant

End Function

Private Function cShape_toString() As Variant

End Function

cRectangle 클래스

Option Explicit
Implements cShape

Public myLength As Double ''going to treat length as d
Public myWidth As Double ''going to treat width as b
Private getIntertiaX As Double

Public Function getArea()
    getArea = myLength * myWidth
End Function

Public Function getInertiaX()
    getIntertiaX = (myWidth) * (myLength ^ 3)
End Function

Public Function getIntertiaY()
    getIntertiaY = (myLength) * (myWidth ^ 3)
End Function

Public Function toString()
    toString = "This is a " & myWidth & " by " & myLength & " rectangle."
End Function

Private Function cShape_getArea() As Variant

End Function

Private Function cShape_getInertiaX() As Variant

End Function

Private Function cShape_getIntertiaY() As Variant

End Function

Private Function cShape_toString() As Variant

End Function

cShape 클래스

Option Explicit

Public Function getArea()
End Function

Public Function getInertiaX()
End Function

Public Function getIntertiaY()
End Function

Public Function toString()
End Function

여기에 이미지 설명 입력


구문의 빠른 수정

인터페이스 ISomeInterface에 다음 이있는 경우 :

Public Sub someMethod()
    ' Interface, no code
End Sub

그런 다음 구현다음 과 같아야합니다.

Implements ISomeInterface

Public Sub ISomeInterface_someMethod()
    '      ^^^^^^^^^^^^^^^  ' If missing: Compile Error 
    ' Code goes here
End Sub

좋은 접근 방식 :

Implements ISomeInterface

Private Sub someMethod()
    ' Business logic goes here
End Sub

Public Sub ISomeInterface_someMethod()
    someMethod ' i.e. Business logic in 1 place: someMethod
End Sub

즉, 다른 답변은 읽을 가치가 있습니다.


인터페이스가 유용 할 수있는 이유와시기를 간단히 이해하는 매우 흥미로운 게시물입니다! 그러나 기본 구현에 대한 마지막 예가 잘못되었다고 생각합니다. drawIDrawable로 인스턴스화 된 square_1 메서드에 대한 첫 번째 호출은 사용자가 제공 한 결과를 올바르게 인쇄하지만 drawcSquare로 인스턴스화 된 square_1 메서드에 대한 두 번째 호출 은 올바르지 않으며 아무것도 인쇄되지 않습니다. 세 가지 다른 방법이 실제로 작동합니다.

IDrawable.cls :

Public Function draw()
    Debug.Print "Interface Draw method"
End Function

cSquare.cls :

Implements IDrawable

Public Function draw()
    Debug.Print "Class Draw method"
End Function

Public Function IDrawable_draw()
    Debug.Print "Interfaced Draw method"
End Function

표준 모듈 :

Sub Main()
    Dim square_1 As IDrawable
    Set square_1 = New IDrawable
    Debug.Print "square_1 : ";
    square_1.draw

    Dim square_2 As cSquare
    Set square_2 = New cSquare
    Debug.Print "square_2 : ";
    square_2.draw 

    Dim square_3 As IDrawable
    Set square_3 = New cSquare
    Debug.Print "square_3 : ";
    square_3.draw
End Sub

결과 :

square_1 : Interface Draw method
square_2 : Class Draw method
square_3 : Interfaced Draw method

참고 URL : https://stackoverflow.com/questions/19373081/how-to-use-the-implements-in-excel-vba

반응형