ASP class topic _asp class

Source: Internet
Author: User
Tags constructor inheritance
Strictly speaking, ASP is not a programming language, so there is no concept of class, we say that ASP class refers to the ASP's programming language VBScript in the application of the ASP class.
ASP class is relatively simple, it does not have the real meaning of the class inheritance, overload, and so on, however, it is still very useful, it reduces the complexity of our programming, so that our program modules more reasonable.
ASP class support What ASP class is relatively simple, it does not have a true meaning class inheritance, overload and other characteristics. It supports the following content:
Public member variables, private member variables, properties
Public member functions, private member functions
constructors, destructors
Create and use one of the simplest ASP classes some beginners do not understand the difference between class and object, for example, the class is like a drawing, the object is like a drawing system to make the kind. This example gives a simple example of an ASP class and object that contains only one member function that interprets the basic format of the class and gives methods for creating and releasing objects. Detailed content ASP class member variables, member functions, constructor destructors This is a relatively complete class explanation, all the explanations are annotated in the way written next to the program. By reading this section, the ASP class has a more comprehensive understanding, you can carry out ASP class module development. Detailed content ASP class attribute ASP class attributes and ASP class public member variables have some of the following characteristics.
You can perform other functions while assigning a value
You can determine the scope of the assignment
You can set a read-only, write-only property
So the ASP class attribute is also more common and important. Detailed content ASP class combination ASP class function is limited, so sometimes we want to implement complex functions, have to use a combination to achieve.
Create one of the simplest ASP classes
Class Cfoo
Sub Printhello ()
Response. Write "Hello world!"
End Sub
End Class

As you can see, the basic structure of the ASP class is:
Class name
Class Code
End Class

Use one of the simplest ASP classes
Dim obj
Set obj = new Cfoo ' Creates an object using set new, and cannot use server because it is not a server component. CreateObject Create
Call obj. Printhello ' object name is added, followed by a member variable, property, or function, which indicates that the member variable, property, or function that called the object will display the Hello world!
Set obj = Nothing ' Frees objects

Constructors cannot be overloaded in an ASP class, so we cannot create classes using statements similar to set obj = new Cfoo ("Param").
the functionality of the ASP class is limited, so sometimes we have to implement complex functions, we have to use a combination to achieve.
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 swim
Call animal.bird.Fly () ' Bird fly
Set animal = Nothing
ASP Class Properties
Class Cfoo
Private PVT

' Property Write
Public Property Let PropName (v)
Pvt = V
End Property

' Property 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) ' Show ' property value
Set obj = Nothing

You can see that the property value is equivalent to building a bridge between the private member variable and the outside, so that both parties can communicate with each other.
One would ask, as with a public member variable, what is the independent meaning of its existence?
1. Can perform other functions while assigning value
The following code, which is equivalent to assigning a value of two member variables
Public Property Let PropName (v)
Pvt = V
Another private member variable name = "Pvt value:" & V
End Property
2, can determine the scope of assignment
If the attribute in the example above is read as follows:
Public Property Let PropName (v)
If Len (v) <= 5 Then
Pvt = V
End If
End Property
Executing obj.propname = "Chivan" will not assign a value to pvt successfully because the string is longer than 5.
3, can be set to read-only, write-only properties
As in the example above, if we remove the Let function, the call to Obj.propname = "Property value" is an error, because the function is only readable.
Class Cfoo
Dim Publicparam ' Declare public member variables with Dim
Private Privateparam ' privacy member variable
' Publicparam = ' public ' cannot be assigned to a member variable within a class tag, outside of a class function
' Const Max_len = 5 ' cannot use const outside of class tags, classes functions

' This function is a constructor that automatically executes when an object is created using set new
Private Sub Class_Initialize ()
Publicparam = "Public" can assign a value to a member variable here
Privateparam = "Private"
End Sub

' This function is a destructor that automatically executes when the object is disposed with set nothing
Private Sub Class_Terminate ()
Response. Write "Release Object"
End Sub

' Public member function (procedure)
' member function (procedure) not required or declared
Sub PrintParam (Index)
If index = 1 Then
Call Printpublicparam ()
ElseIf index = 2 Then
Call Printprivateparam ()
End If
End Sub

' Private member function (procedure)
' Just 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, assigning values for Publicparam, Privateparam
Obj.publicparam = "New Public"
Obj.privateparam = "New Private" will have an error because Privateparam is a private variable
Call obj. PrintParam (1) ' shows the value of the Publicparam
Call obj. Printpublicparam () ' will cause an error because printpublicparam is a private function
Set obj = Nothing ' automatically calls the Class_Terminate function, displaying ' release object '
Related Article

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.