ASP class usage
ASP class
Strictly speaking, ASP is not a programming language, so there is no such concept. Here we say that ASP class refers to the class used by VBScript in ASP.
ASP class is relatively simple, it does not have the inheritance, overload and other features of the true meaning class, even so, it is still very useful, it reduces the complexity of our programming, make our program module more reasonable.
ASP class supports ASP class is relatively simple, it does not have the inheritance, overload and other characteristics of the true meaning class. It supports the following content:
Public member variables, Private member variables, attributes
Public and private member functions
Constructor and destructor
Creating and using a simple ASP class some beginners do not understand the differences between classes and objects. For example, a class is like a drawing, and an object is like a physical object created based on a drawing. This example provides a simple example of an ASP class and an object. This class contains only one member function to explain the basic format of the class and provides methods for creating and releasing objects. For details, ASP class member variables, member functions, and constructor are a complete class explanation. All the explanations are annotated and written next to the program. By reading this section, you can have a comprehensive understanding of ASP classes and Develop ASP class modules. Details ASP attributes have the following characteristics compared with ASP public member variables.
Other functions can be executed while assigning values.
You can determine the value range.
Read-only and write-only attributes can be set.
Therefore, ASP attributes are commonly used and important. Details ASP class combination ASP class functions are limited, so sometimes we have to implement complicated functions with combinations.
Create a simple ASP class
Class cfoo
Sub printhello ()
Response. Write "Hello world! "
End sub
End Class
It can be seen that the basic structure of ASP classes is:
Class Name
Class Code
End Class
Use a simple ASP class
Dim OBJ
Set OBJ = new cfoo ': Use set new to create an object. Because it is not a server component, server. Createobject cannot be used to create an object.
Call obj. printhello: add the name of the object, and call the member variable, attribute, or function to call the member variable, attribute, or function of the object. Hello world is displayed!
Set OBJ = nothing 'release the object
The constructor cannot be overloaded in ASP classes, so we cannot use statements similar to set OBJ = new cfoo ("Param") to create classes.
ASP functions are limited, so sometimes we have to use combinations to implement complicated functions.
Class cfish
Sub swim ()
End sub
End Class
Class cbird
Sub fly ()
End sub
End Class
Class canimal
Dim fish
Dim bird
Private sub class_initialize ()
Set fish = new cfish
SET bird = new cbird
End sub
Private sub class_terminate ()
Set fish = nothing
SET bird = nothing
End sub
End Class
Dim animal
Set animal = new canimal
Call animal. Fish. Swim () 'fish game
Call animal. Bird. Fly () 'bird fly
Set animal = nothing
ASP attributes
Class cfoo
Private PVT
'Write attributes
Public property let propname (V)
Pvt = V
End Property
'Attribute read
Public property get propname ()
Propname = PVT
End Property
End Class
Dim OBJ
Set OBJ = new cfoo
OBJ. propname = "property value"
Response. Write (obj. propname) 'displays "property value"
Set OBJ = nothing
It can be seen that the attribute value is equivalent to a bridge between the private member variable and the outside, so that both parties can communicate with each other.
Some people will ask what is the significance of the existence of a property, just like a public member variable?
1. Other functions can be executed while assigning values
The following Code assigns values to both member variables:
Public property let propname (V)
Pvt = V
Another private member variable = "PVT value:" & V
End Property
2. The value range can be determined.
In the preceding example, the attribute is written as follows:
Public property let propname (V)
If Len (v) <= 5 then
Pvt = V
End if
End Property
Executing obj. propname = "trillion" won't assign a successful value to Pvt, because the length of the string exceeds 5.
3. Read-only and write-only attributes can be set.
In the above example, if we remove the let function, an error occurs when calling obj. propname = "attribute value" because the function is only readable at this time.
Class cfoo
Dim publicparam 'Declare Public member variables with dim
Private privateparam use private to declare private member variables
'Publicparam = "public" 'cannot assign values to member variables within the class tag or outside the class function.
'Const max_len = 5' cannot be used outside the class mark or function class
'This function is a constructor. It is automatically executed when an object is created using set new.
Private sub class_initialize ()
Publicparam = "public" 'can assign values to member variables here
Privateparam = "private"
End sub
'This function is an destructor. It is automatically executed when an object is released using set nothing.
Private sub class_terminate ()
Response. Write "Release object"
End sub
'Public member function (process)
'The member functions (procedures) are neither required nor can be affirmed
Sub printparam (INDEX)
If Index = 1 then
Call printpublicparam ()
Elseif Index = 2 then
Call printprivateparam ()
End if
End sub
'Private member function (process)
'Only one more private than the public member function (process)
Private sub printpublicparam ()
Response. Write publicparam
End sub
Private sub printprivateparam ()
Response. Write privateparam
End sub
End Class
Dim OBJ
Set OBJ = new cfoo automatically calls the class_initialize function and assigns values to publicparam and privateparam.
OBJ. publicparam = "New Public"
OBJ. privateparam = "new private" 'will cause an error because privateparam is a private variable
Call obj. printparam (1) 'displays the publicparam Value
Call obj. printpublicparam () 'will cause an error because printpublicparam is a private function
Set OBJ = nothing 'automatically calls the class_terminate function and displays "Release object"