In-depth VBS class and Object

Source: Internet
Author: User

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:

  1. Class TestClass
  2. Private Sub Class_Initialize 'sets the Initialize event.
  3. MsgBox ("TestClass started ")
  4. End Sub
  5. Private Sub Class_Terminate 'sets the Terminate event.
  6. MsgBox ("TestClass terminated ")
  7. End Sub
  8. End Class
  9. Set X = New TestClass 'to create a TestClass instance.
  10. 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.

  1. Class Circle
  2. Public r
  3. Private mArea
  4. Public Sub Init ()
  5. R =
  6. End Sub
  7. Public Function Area ()
  8. If mArea = "" Then mArea = Atn (1) * 4 * r
  9. Area = mArea
  10. End Function
  11. Private Sub EchoInfo ()
  12. MsgBox "The circle radius is:" & r & vbCrLf & "The area of the circle is:" & mArea
  13. End Sub
  14. End Class
  15. Set c = New Circle 'to create a TestClass instance.
  16. C. Init 2
  17. 'Init is the name of the constructor in Python. You can define other names as you like.
  18. '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.
  19. MsgBox c. r
  20. MsgBox c. Area ()
  21. 'Msgbox c. mArea
  22. 'Cannot be accessed because this field is private and can only be accessed within the class.
  23. 'Msgbox c. EchoInfo ()
  24. 'Cannot be accessed because this method is private and can only be accessed within the class
  25. Set c = Nothing 'delete an instance

Copy code

The following is an example related to attributes:

  1. Class Student
  2. Private mAge, mName, mNumber, mSex, mLover, mYear, mRank
  3. 'It is best to define all fields as private, so that you can control the read and write operations of fields more effectively.
  4. 'And can provide some other computing processing during reading and writing.
  5. 'Assume that Lover is also a student, then this field is
  6. 'The object type field, please note how its attributes are implemented
  7. Dim Belief 'life creed, which is not accessible here
  8. Sub Init (namestr, sexstr, ageint, rankint)
  9. If mName = "" Then ", the instance can be initialized only once.
  10. MName = namestr
  11. MSex = sexstr
  12. MAge = ageint
  13. MRank = rankint
  14. Randomize
  15. MNumber = Int (rndx 10000 + 1)
  16. MYear = Year (Date)
  17. 'The mLover field Initialization is not provided here. After all, love at first sight is too small.
  18. Else
  19. MsgBox "No, you can't initialize a instance twice"
  20. End If
  21. End Sub
  22. Property Get Rank ()
  23. Rank = mRank
  24. End Property
  25. Property Let Rank (value)
  26. MRank = value
  27. End Property
  28. '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
  29. 'Is a value type, not an object type (usually referred to as a reference type). Therefore, Set statements cannot be used for external values.
  30. Property Get Age ()
  31. If mYear <Year (Date) Then
  32. MYear = Year (Date)
  33. MAge = mAge + 1
  34. End If
  35. Age = mAge
  36. End Property
  37. 'Age will only change as the year increases. After all, people live in the same time and space.
  38. Public Default Property Get Name ()
  39. Name = mName
  40. End Property
  41. 'The Student name cannot be changed by default, but you can also change the name. We recommend that you define it in the class.
  42. 'Conservative renaming Method
  43. 'Here Name is defined as the Default attribute. Note that the Default keyword must have a Public keyword when defining the Default attribute.
  44. Property Get Sex ()
  45. Sex = mSex
  46. End Property
  47. 'Student gender, of course, will not be changed once initialized, unless it hurts to change
  48. Property Get Lover ()
  49. Set Lover = mLover
  50. End Property
  51. Property Set Lover (value)
  52. Set mLover = value
  53. End Property
  54. 'You have the Set attribute. This child (female) is really romantic. If you are special, Private it.
  55. 'It is also possible to write the Property Let here, but the Set stu1.Lover = stu2 should be removed later.
  56. '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
  57. ', 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
  58. 'Reference type (Object Type). If the Set attribute is defined above, the Set statement is required.
  59. 'From the above two attributes of Lover and Rank, we can conclude that:
  60. '(1) whether it is encapsulation of the reference type field or encapsulation of the Value Type field, you can use Property Let, and Set
  61. 'Only appears when you encapsulate a reference type field
  62. '(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
  63. 'Defines whether the value assignment method is determined by the Property Let or Property Set, that is, the Set statement is not used.
  64. Function Info ()
  65. Info = "Student Information:" & vbCrLf &_
  66. "Student ID:" & mNumber & vbCrLf &_
  67. "Name:" & mName & vbCrLf &_
  68. "Gender:" & mSex & vbCrLf &_
  69. "Age:" & Age & vbCrLf &_
  70. "Ranking:" & mRank & vbcrlf &_
  71. "Couples:" & mLover
  72. End Function
  73. 'Here, Age is obtained through attributes to ensure that the correct Age is obtained each time.
  74. 'The write attribute of the Age is not defined, which protects the field and avoids unauthorized rewriting.
  75. 'The external read can only be accessed through the read attribute of Age, so the validity of the external read Age is ensured.
  76. 'Mlover. Name can be output directly using mLover, because its Name attribute is the default attribute.
  77. End Class
  78. Dim stu1, stu2
  79. 'It is best to define variables to prevent the impact of global variables on user-defined functions.
  80. Set stu1 = New Student
  81. Set stu2 = New Student
  82. Stu1.Init "Jobs", "BigBoy", 23, 13
  83. 'Stu1. Age = 32
  84. 'Here, an error is returned because the write attribute of Age is not defined, and the Age cannot be changed externally by assigning values.
  85. Stu2.Init "Joliy", "Girl", 21, 10
  86. Set stu1.Lover = stu2
  87. MsgBox stu1.Info is accessible. The description is implicitly Public.
  88. Stu1.Rank = 8' accessible, indicating that the property is implicitly Public
  89. Stu1.Belief = "No pains, no gains"
  90. MsgBox stu1.Belief is accessible, indicating that the field is implicitly Public
  91. MsgBox "use default attributes:" & vbcrlf & stu1 & vbcrlf & stu2
  92. Set stu1 = Nothing
  93. 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.

  1. Class TestClass
  2. Sub Class_Initialize 'sets the Initialize event.
  3. MsgBox ("TestClass started ")
  4. End Sub
  5. Sub Class_Terminate 'sets the Terminate event.
  6. MsgBox ("TestClass terminated ")
  7. End Sub
  8. End Class
  9. Set X = New TestClass 'to create a TestClass instance.
  10. X. Class_Initialize 'can run normally
  11. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.