수색…


클래스 만들기

Class Car

    Private wheels_
    Private distances_

    ' Property getter
    Public Property Get Wheels()
        Wheels = wheels_
    End Property
    
    ' Property setter
    Public Property Let Wheels(v)
        wheels_ = v
    End Property
    
    ' Parameterless Constructor
    Public Sub Class_Initialize()
        distances_ = Array(0)
    End Sub
    
    ' Method
    Public Function GetTotalDistance()
        dim d
        'GetTotalDistance = 0
        For Each d in distances_
            GetTotalDistance = GetTotalDistance + d
        Next
    End Function
    
    ' Void Method
    Public Sub Drive(distance)
        distances_(ubound(distances_)) = distance
        Redim Preserve distances_(ubound(distances_)+1)
    End Sub
    
End Class

클래스 인스턴스 사용

' Initialize the object
Dim myCar
Set myCar = new Car

' Setting a property
myCar.Wheels = 4

' Getting a property value
wscript.echo myCar.Wheels

' Using a subroutine in a class
myCar.Drive 10
myCar.Drive 12

' Using a function in a class
wscript.echo myCar.GetTotalDistance()    ' returns 22

파라미터 화 된 생성자를 에뮬레이트하는 글로벌 팩토리 기능

' Making a factory with parameter to the class
Public Function new_Car(wheels)
    Set new_Car = New Car
    new_Car.Wheels = wheels
End Function

' Creating a car through a factory
Dim semiTrailer
Set semiTrailer = new_Car(18)

매개 변수가있는 생성자를 에뮬레이트하는 Init 메서드

Class Car
    ...
    ' Parameterless Constructor
    Public Sub Class_Initialize()
        distances_ = Array(0)
    End Sub

    ' Default initialization method that can be invoked without
    ' explicitly using the method name.
    Public Default Function Init(wheels)
        wheels_ = wheels
        Set Init = Me
    End Function
    ...
End Class

Set car1 = (New Car)(18)       ' implicit invocation
Set car2 = (New Car).Init(8)   ' explicit invocation

외부 클래스 파일을 스크립트에로드 중입니다.

Dim classFile : classFile = "carClass.vbs"
Dim fsObj : Set fsObj = CreateObject("Scripting.FileSystemObject")
Dim vbsFile : Set vbsFile = fsObj.OpenTextFile(classFile, 1, False)
Dim myFunctionsStr : myFunctionsStr = vbsFile.ReadAll
vbsFile.Close
Set vbsFile = Nothing
Set fsObj = Nothing
ExecuteGlobal myFunctionsStr

Dim car1 : Set car1 = (New Car)(18)


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow