At an example of Miles, as an example of a class design, I put it out of my design date class, write into this article for everyone to do a reference. I also hope that this article can throw a brick, let everyone write better, more class. If you have any place in the text, please refer to it. (1) Simply said that the CBScript custom class, the class is an object, it has attributes and methods. Customized in VBScript is more than C , Java is much simpler. Next, a date class is designed to show a combined form object. While designing, we will also show how to design a custom class. 1. Define the grammar of the class: Class .... End Class Class and End Class use to identify the beginning and end of the class block, the Class identifier followed by the class name. Now we name the date class to be designed: DateClass syntax is as follows: Class DateClass ... End Class
Use new operators when you create new objects in VBScript. For example: set newdate = new dateclass
2, attributes and methods: private, public, and proty private use to define variables and functions that can only be accessed inside the class; public use to define the interface of the class, which is the properties and methods available to external access, not to become Private and Publician members, will be treated as public; there is time, we have to access the outside, but to specify the method of access, this time, use the property, the Property syntax as follows: public property [let | set | get] AA ( ...) ... End Property
Property must be used with let, set or get. The following: Let settings, such as: user.age = 100 set setting object, such as: set user.myObject = NOBJECT GET gnap, such as: myage = user.age
3, the properties and methods of design date classes Now we will design the date of the date class. To display the date, a classdate is defined, and the date is saved. It is public that allows the user to change it outside the class; then design a function to display the date, name: datedisplay, type as public, no parameters. The procedure is as follows: <% class dateclass
Public ClassDate
Public function datedisplay ()
END FUNCTION
END CLASS%>
4. Add to display the date of the display Now let's join the dateDisplay code, the program is as follows: <% class dateclass
Public ClassDate
Public Function dateDisplay () If there is no specified date, set it to the current date if classdate = "" "" = year () end if yy = year (classdate) mm = month (classdate) DD = day (classdate) response.write "& vbcrlf response.write"
To save the above code as dateclass1.asp, now we have written this date class, let's take a look at the case used, call class include Use this class in other programs, as long as INCLUDE puts dateclass1.asp Come in. Below we write a test program, the file name is TEST1.ASP <% set newdate = new dateclass response.write "call display:
" newdate. datedisplay response.write "value displayed classdate
of:
" response.write newdate.classdate response.write "
classdate set value:
" newdate.classdate = cdate ( "2005/6 / 15 ") 'The previous sentence can also be written:' newdate.classdate =" 2005/6/15 "response.write"
call display:
"NewDate.datedisplay set newdate = Nothing%> Putting two files Put it in the same directory, run Test1.asp, you should have seen the result. However, this design has some problems: 1. If the user specified ClassDate is not a date type, then the date will become January 1, 1900; 2. If you display multiple dates, the name of the form object cannot be the same; 3, it is best to join CSS; 4, it is best to join the judge of the leap year; 5, not 31 days every month; with these problems, we will continue ........
I am busy looking for work until I am free. Now continue our design of the VBScript class. The last time we have designed a simple date class, but there are still some uses.
The problem. Now we change our design for these questions. This time we have to solve the problem is: 1. If the user specified ClassDate is not a date type, then the date will become January 1, 1900; 2. If you display multiple dates, the name of the form object cannot be the same; 3, it is best to join CSS; discuss one below, in the last time we design Dataclass, we define a public type ClassDate to represent the date, users can freely value ClassDate and
Read, this is not possible to determine whether the value assigned by the user is correct. Now we redefine a variable CLDATE to change to the Private type, so that the user can't access CLDATE directly.
. As follows: Private CLDATE
In order to allow users to access ClassDate, the last time I mentioned Property, now we use it to construct two functions, one is to let users pay ClassDate
Assigning the function called classdate. Define as follows: PBULIC Property Let Classdate (fdate) ... End Property
Another function is to let the user read CLDATE, defined as follows: Public property get classdate () ... End Property
Ok, after defining the function, you can write the code. In let classdate, it is mainly to determine whether the user enters is not a date type, as follows: Public property let classdate (fdate) if typeename (fdate) = "date" Then Cldate = fdate end if End Property In Get ClassDate, mainly assigns the value of CLDATE to the function: as follows: Public property get classdate () ClassDate = CLDATE End Property
If you didn't assign a CLNAME? That is, when this class is just generated, ClName is an empty value, here is introduced to automatic process when initialization and termination: CLAS_Initialize
And Class_Terminate. The first process is automatically executed in the generated class, and the second is automatically executed when the class is eliminated. As long as the first process is used here. As follows: private sub class_initialize () CLDATE = now () End Private
In this way, when the class is generated, if the CLNAME is not assigned, the ClName is the current date.
Second, in order to display multiple dates in the form, we must distinguish the date object each time, that is, take a name. Here we define a private type variable clname
Define the name of the generated form control. Definition as follows: Private ClName
Similarly, let users access the clname variable, and define two functions, methods, and classdate. The code is as follows: Public property let classname (fname) if fname <> "" ""
Public property get classname () classname = Clname End Property
When the form date control is displayed each time, you should add name to the control, we name each year, month, day respectively: clname & "clname" clname & "_MONTH" ClName & "_day", we let ClName default For "datename", that is, in Class_Initialize, the code is improved, the Class_initialize is as follows: private sub class_initialize () CLDATE = now () clname = "datename" End Private
In order to make the value of ClName after each display, we add numbers to the clname. Here we introduce a private variable clcount, when generating classes, Clcount
The value is "0", if the value is assigned to ClName, then Clcount is also set to 0 code as follows: Private Clcount
Private sub class_initialize () CLDATE = now () clname = "datename" clcount = 0 End Sub
Public Property Let ClassName (FNAME) If FNAME <> "" The ClName = FNAME CLCOUNT = 0 End if End Property 3, finally, we introduce a private variable clcss to define the CSS of the form control. As long as you provide a function of assignment, you can read the CSS is not used here. The code is as follows: Private CLCSS
Public Property Let ClassCSS (FCSS) IF FCSS <> "" "THEN CLCSS = FCSS End IF"
Fourth, good, now to modify the display date function, and let the value of Clcount 1 after each display. The modified file is saved as DateClass2.asp, the full code is as follows: <% class dateclass
'Defining Variables Private CLDATE PRIVATE CLNAME Private Clcount Private CLCSS
'Initialization Private Sub Class_initialize () CLDATE = Date () ClName = "Datename" Clcount = 0 End Sub
'Access CLDATE PUBLIC Property Let Classdate (fdate) = "DATE" TEN CLDATE = fdate end if End Property
Public property get classdate () Classdate = CLDATE End Property
'Access ClName Public Property Let ClassName (FNAME) IF FNAME <> "" "THEN ClName = Fname Clcount = 0 End If End Property
Public property get classname () classname = Clname End Property
'Access CLCSS Public Property Let ClassCSS (FCSS) IF FCSS <> "" "THEN CLCS = FCSS End If"
'Modified display date function public function datedisplay ()' gives the displayed form control Auto number if Clcount> 0 Then cname = clname & cstr (clcount-1) else cname = clname end if Yy = Year (classdate) mm = month (ClassDate) response.write " "& vbrlf response.write"