With the advent of the VBScript 5.0 engine, developers can use Vbscript to create classes. Just like other object-oriented languages, let's take a look at object-oriented programming:
1. object-oriented programming:
Object-Oriented Programming (OOP) is a kind of programming methodology. In this method, entity is regarded as the image of a real thing. an object has attributes (states) and methods (Actions). attributes are used to describe the characteristics of an object. methods are inherent behavior actions of an object.
Note:
The real object-oriented programming should be further defined as a new object with the ability to inherit existing object attributes and methods, and to dynamically bind and derive new objects, oop In VBScript does not have these two elements, so it cannot be said to be a real object-oriented programming language.
2. Objects and Instances
To thoroughly understand object-oriented programming, we must first understand the differences between objects and object instances:
Object: for example, the connection object of ADO is a concept of image extraction. It does not exist physically. On the contrary, it is only used as a template for instance creation.
Instance: A physical entity of an object. For example:
ADODB. Connection is an object, and objconn is an instance of ADODB. connection. The Code is as follows:
Dim objconn
Set objconn = server. Createobject ("ADODB. Connection ")
To call an object method and set or obtain its property value, you must own an object instance.
3. encapsulate complex code
Object-Oriented Programming can encapsulate complex development of specific tasks. for example, suppose you need to open a text file on the website, calculate the number of lines of the file, and record the obtained information in another file. if you do not use the object-oriented method, you have to repeat the following steps every time you implement this task:
A: open a text file.
B: calculates the number of lines in a text file.
C: close this file.
D: Open the record file.
E: record the number of rows in step B.
F: Close the record file.
We can encapsulate the above process in a single object. Let's see what this object has?
Object: logtextfilelines
Property: textfilepath // used to set the physical path of the record file on the disk
Method: updatelog // calculates the number of rows in a text file and writes the result to an appropriate record file.
Once this object is created, the above steps become very simple:
Dim objlog
Set objlog = new logtextfilelines
Objlog. textfilepath = "C:/log/linecount. log"
If objlog. updatelog ("C:/TXT/scott.txt") then
Response. Write "logged successfully! "
Else
Response. Write "an error when attempting to log"
End if
The black box method is used to process the entire process, which will greatly save the development time and, most importantly, reduce the error generation.
4. Use classes in VBScript
The class in VBScript can only have one Constructor (constructor: The process of Automatic execution when the class instance is created). Even worse, the VBScript constructor cannot use any input parameter, in addition, VBScript does not support inheritance (inheritance ).
Creation class:
You can use the class statement. The syntax format is as follows:
Class classname
... Class attributes and Methods
End Class
Initialization and termination events:
When creating a class, pay attention to two important event handlers: initialization and termination.
When the keyword "new" is used to create a class instance, the initialization event is triggered. For example:
Dim objlog
Set objlog = new logtextfilelines
When a class instance is released, the termination event is triggered. You can declare it explicitly:
Set objlog = nothing
It can also be automatically released implicitly when it exceeds the scope.
You can customize these event processors in the class. the initialization event processor can be used to initialize the attributes of a class or perform things that must be done when a task is started. an initialization event is generally called a constructor in a class.
The termination event can be used to close a task. It is generally called a destructor. Code:
Class logtextfilelines
Private sub class_initialize () // Initialization
....
End sub
Private sub class_terminate () // terminate
...
End sub
End Class
5. attributes, methods, member variables, and member functions
From the perspective of a terminal developer (only developers of created classes are used: End developer; Class developers --- programmers who create classes for other developers: Class Developer, A class contains properties and methods ).
The property is a variable, which is used by the terminal developer to set the status of the class.
The method is a function provided by the class. Terminal developers can use it to execute certain tasks.
For example:
The ADODB. connection object includes the attributes describing the instance status of the object: connectionstring, connectiontimeout, and provider.
It also includes methods that can perform certain operations: open, close
From the perspective of class developers, there can also be variables and functions that terminal developers cannot directly call. these "hidden" variables and functions are called member variables and member functions respectively)
6. Public and private
One of the goals of object-oriented programming is to provide a black box so that all specific implementation details are transparent to terminal developers. to implement encapsulation, VBScript allows you to hide object methods and attributes. but remember that objects should be transparent to terminal developers. All creators of objects must be able to prevent users from directly calling some methods or setting some attributes.
To create a member function and a member variable, you must add the keyword private before the definition of the member variable and the member function. A member function can only be called by other methods or members in the same class. member variables can only be accessed by methods and member functions in the class or through the property get, property let, and property set statements.
To create an attribute or method, you must add the Keyword: public before defining the attribute and method. if you do not explicitly declare that an attribute or method is a public or private member, this attribute or method is defined as a public member.
Sample Code:
Class logtextfilelines
Public textfilepath
Public sub updatelog (PARAM)
...
End sub
Private a_private_property
Private sub a_private_method ()
...
End sub
End Class
Note:
The dim statement is omitted no matter whether it is private or public before the variable. If you choose not to explicitly declare whether the variable is private or public, you must add the dim statement before the attribute name, this will automatically set the variable to public. however, the best practice is to explicitly declare the variables or methods as private or public.
The above article is taken from:
O 'reiily's designing Active Server Pages
For the syntax of property get, property let, and property set, see:
VBScript/VBScript language reference/objects and collections/Class Object in Windows scripting technologies (script56.chm)
Msdn Library:
The http://msdn2.microsoft.com/en-us/library/23t9k18c (vs.85). aspx