VBS can also be written as follows:
1. The VBS class has two types of events:
(1) Initialize event, which occurs (new) when a class instance is created.
Private Sub Class_Initialize ()
Statements
End Sub
When class initialization,StatementsIt consists of zero or more code statements to be run.
(2) Terminate event: This event occurs when the instance of the associated class is terminated (destroyed.
Private Sub Class_Terminate ()
Statements
End Sub
When class initialization,StatementsIt consists of zero or more code statements to be run.
The following is an example of a class event:
- Class TestClass
- Private Sub Class_Initialize 'sets the Initialize event.
- MsgBox ("TestClass started ")
- End Sub
- Private Sub Class_Terminate 'sets the Terminate event.
- MsgBox ("TestClass terminated ")
- End Sub
- End Class
- Set X = New TestClass 'to create a TestClass instance.
- Set X = Nothing 'delete the instance.
Copy code
Abstract:
A. The event Method format of the class is fixed, the event name is fixed, the event can only be Sub, not Function, and the event has no parameters;
B. Class instances are created through New and destroyed by Nothing;
C. Remember that all class instances are objects. Remember to use the Set statement during creation and destruction. If there is no Set, it is wrong.
D. Neither of these two events is necessary, that is, the implementation code of these two events cannot be provided during class writing;
E. These two events have great functions. you can do some other work when initializing objects and destroying objects in these two events.
F. Because neither of the two VBS events accepts the parameter, the object cannot be initialized when the New object is created. You can set another public process to complete this task.
2. The class can have public and private fields:
(1) Public fields can be accessed externally. Private fields cannot:
(2) An array field can be defined and can be a dynamic array.
3. The class can have public or private attributes:
(1) public properties can be called externally, and private properties cannot.
(2) Use Property Get to define read attributes, and use Property Set and Property Let to define write attributes.
(3) when defining a write attribute, if this attribute encapsulates an object field, this write attribute must be a Property Set
(4) attributes do not need to appear in pairs. This can cause read-only or write-only attributes. Of course, this can also happen to any Private party.
4. classes can have public and private methods:
(1) Public methods can be called externally, while private methods cannot.
- Class Circle
- Public r
- Private mArea
- Public Sub Init ()
- R =
- End Sub
- Public Function Area ()
- If mArea = "" Then mArea = Atn (1) * 4 * r
- Area = mArea
- End Function
- Private Sub EchoInfo ()
- MsgBox "The circle radius is:" & r & vbCrLf & "The area of the circle is:" & mArea
- End Sub
- End Class
- Set c = New Circle 'to create a TestClass instance.
- C. Init 2
- 'Init is the name of the constructor in Python. You can define other names as you like.
- 'Other languages, such as C # And C ++, all use class names as the name of the constructor. Obviously there is no simple and easy to remember Init.
- MsgBox c. r
- MsgBox c. Area ()
- 'Msgbox c. mArea
- 'Cannot be accessed because this field is private and can only be accessed within the class.
- 'Msgbox c. EchoInfo ()
- 'Cannot be accessed because this method is private and can only be accessed within the class
- Set c = Nothing 'delete an instance
Copy code
The following is an example related to attributes:
- Class Student
- Private mAge, mName, mNumber, mSex, mLover, mYear, mRank
- 'It is best to define all fields as private, so that you can control the read and write operations of fields more effectively.
- 'And can provide some other computing processing during reading and writing.
- 'Assume that Lover is also a student, then this field is
- 'The object type field, please note how its attributes are implemented
- Dim Belief 'life creed, which is not accessible here
- Sub Init (namestr, sexstr, ageint, rankint)
- If mName = "" Then ", the instance can be initialized only once.
- MName = namestr
- MSex = sexstr
- MAge = ageint
- MRank = rankint
- Randomize
- MNumber = Int (rndx 10000 + 1)
- MYear = Year (Date)
- 'The mLover field Initialization is not provided here. After all, love at first sight is too small.
- Else
- MsgBox "No, you can't initialize a instance twice"
- End If
- End Sub
- Property Get Rank ()
- Rank = mRank
- End Property
- Property Let Rank (value)
- MRank = value
- End Property
- 'The write attribute here can only be a Property Let, not a Property Set, because the attribute to be assigned externally is an int type
- 'Is a value type, not an object type (usually referred to as a reference type). Therefore, Set statements cannot be used for external values.
- Property Get Age ()
- If mYear <Year (Date) Then
- MYear = Year (Date)
- MAge = mAge + 1
- End If
- Age = mAge
- End Property
- 'Age will only change as the year increases. After all, people live in the same time and space.
- Public Default Property Get Name ()
- Name = mName
- End Property
- 'The Student name cannot be changed by default, but you can also change the name. We recommend that you define it in the class.
- 'Conservative renaming Method
- 'Here Name is defined as the Default attribute. Note that the Default keyword must have a Public keyword when defining the Default attribute.
- Property Get Sex ()
- Sex = mSex
- End Property
- 'Student gender, of course, will not be changed once initialized, unless it hurts to change
- Property Get Lover ()
- Set Lover = mLover
- End Property
- Property Set Lover (value)
- Set mLover = value
- End Property
- 'You have the Set attribute. This child (female) is really romantic. If you are special, Private it.
- 'It is also possible to write the Property Let here, but the Set stu1.Lover = stu2 should be removed later.
- 'From this example, we can see that the internal definitions of the properties defined by Property Let and Property Get are the same as those of the Set statement, but in the external
- ', If the Let attribute is defined above, the Set statement cannot be used to assign values, although the value assigned to the Lover attribute is
- 'Reference type (Object Type). If the Set attribute is defined above, the Set statement is required.
- 'From the above two attributes of Lover and Rank, we can conclude that:
- '(1) whether it is encapsulation of the reference type field or encapsulation of the Value Type field, you can use Property Let, and Set
- 'Only appears when you encapsulate a reference type field
- '(2) when defining a class, we can determine whether to use Set or Let based on the external value assigned to the attribute type. After the definition is completed
- 'Defines whether the value assignment method is determined by the Property Let or Property Set, that is, the Set statement is not used.
- Function Info ()
- Info = "Student Information:" & vbCrLf &_
- "Student ID:" & mNumber & vbCrLf &_
- "Name:" & mName & vbCrLf &_
- "Gender:" & mSex & vbCrLf &_
- "Age:" & Age & vbCrLf &_
- "Ranking:" & mRank & vbcrlf &_
- "Couples:" & mLover
- End Function
- 'Here, Age is obtained through attributes to ensure that the correct Age is obtained each time.
- 'The write attribute of the Age is not defined, which protects the field and avoids unauthorized rewriting.
- 'The external read can only be accessed through the read attribute of Age, so the validity of the external read Age is ensured.
- 'Mlover. Name can be output directly using mLover, because its Name attribute is the default attribute.
- End Class
- Dim stu1, stu2
- 'It is best to define variables to prevent the impact of global variables on user-defined functions.
- Set stu1 = New Student
- Set stu2 = New Student
- Stu1.Init "Jobs", "BigBoy", 23, 13
- 'Stu1. Age = 32
- 'Here, an error is returned because the write attribute of Age is not defined, and the Age cannot be changed externally by assigning values.
- Stu2.Init "Joliy", "Girl", 21, 10
- Set stu1.Lover = stu2
- MsgBox stu1.Info is accessible. The description is implicitly Public.
- Stu1.Rank = 8' accessible, indicating that the property is implicitly Public
- Stu1.Belief = "No pains, no gains"
- MsgBox stu1.Belief is accessible, indicating that the field is implicitly Public
- MsgBox "use default attributes:" & vbcrlf & stu1 & vbcrlf & stu2
- Set stu1 = Nothing
- Set stu2 = Nothing
Copy code
Abstract:
1. When you use the Default keyword to define the Default attribute, you must explicitly specify the attribute as Public, although the implicit attribute is Public.
2. Set conditions in the object constructor to ensure that the initialization can only be performed once.
3. (1) whether it is encapsulation of the reference type field or encapsulation of the Value Type field, the Property Set
It can only be used to encapsulate reference type fields. In this case, you can still use the Property Set
(2) when defining a class, we can determine whether to use Set or Let based on the external value assigned to the attribute type, because if we want to assign a value
If the type of the attribute value is the value type, you do not need the Property Set in any case. After the Property is defined
Defines whether the value assignment method is determined by the Property Let or Property Set, that is, the Set statement is not used.
4. Element accessibility in the class:
(1) When the attribute is not described, it is regarded as Public, but the Public must be explicitly specified when the Default attribute is defined.
(2) When methods and fields do not explicitly specify their accessibility, they are considered Public
(3) as shown in the following example, only two events of the class can be accessed without being specified. They are also considered Public and can be called externally.
- Class TestClass
- Sub Class_Initialize 'sets the Initialize event.
- MsgBox ("TestClass started ")
- End Sub
- Sub Class_Terminate 'sets the Terminate event.
- MsgBox ("TestClass terminated ")
- End Sub
- End Class
- Set X = New TestClass 'to create a TestClass instance.
- X. Class_Initialize 'can run normally
- Set X = Nothing 'delete the instance.
Note: In the above example, even if the last sentence is removed, the Class_Terminate event will still occur and will be triggered by the interpreter before the script ends.