In fact, the section on Function and SUB statements in the MSDN VBScript document mentions the Default keyword:
Copy Code code as follows:
Default
Used only and the public keyword in a Class blocks to indicate this Function procedure is the "default method" for the C Lass. An error occurs if more than one Default procedure be specified in a class.
Default can only be used with the Public keyword in a class statement block to indicate that a function procedure is the method of the class. If more than one procedure in a class is defined as Default, an error occurs.
A simple example:
Copy Code code as follows:
Class MyClass
Public Default Function SayHello (name)
SayHello = "Hello," & name
End Function
End Class
Set o = New MyClass
MsgBox O ("demon")
Many object-oriented languages can use constructors to initialize objects of a class, but the VBS does not have a constructor concept, but simply provides a class initialization event to initialize the object:
Copy Code code as follows:
Class TestClass
' Setup Initialize event.
Private Sub Class_Initialize
MsgBox ("TestClass started")
End Sub
' Setup Terminate event.
Private Sub Class_Terminate
MsgBox ("TestClass terminated")
End Sub
End Class
' Create an instance of TestClass.
Set X = New TestClass
' Destroy the instance.
Set X = Nothing
Although it looks like a constructor, it cannot take arguments, and there is no way to initialize an object with a specific parameter, as in other languages.
With the Default keyword, we can simulate the function of implementing a constructor:
Copy Code code as follows:
' Author:demon
' Date:2011/09/29
' website:http://demon.tw
Class Rectangle
Private Height, Width
Public Default Function Construtor (H, W)
Height = H:width = W
Set Construtor = Me
End Function
Public Property Get Area
Area = Height * Width
End Property
End Class
' Does it look like a constructor?
Set r = (New Rectangle) (6, 8)
MsgBox R.area
Reference Links: VBScript ' s default keyword
Original: http://demon.tw/programming/vbs-default-keyword.html