Use classes in VBScript (2)

zhaozj2021-02-16  87

Create an object

When you create an object type (class) in VBScript, you must first know that this is really easy! I am self-study in a afternoon, just read the reference book for Microsof VB Script, but must admit that this book is not the most readable document.

Beginners need to install the VBScript 5.0 engine and can be downloaded in Microsoft's Scripting Site.

Let's look at the code. The definition of the class is very similar to the function and sub-process. Start behavior class , the end is End Class, all object definitions written in the intermediate department. Now we can build the first class in the way, and do not implement any features.

Class 4GuystestObject

END CLASS

This is not the matter, but when you write the following code, you will create an instance of an object:

DIM ObjtestObject

Set objtestObject = New 4GuystestObject

Set objtestObject = Nothing

Obviously, we can't use objects now, now I will explain how to define attributes and methods in the object.

The most basic based on objects can be used, which is to create a set of data. For example, if you want to build the time, date, and video program title, you can create an object that contains properties "StartTime", "ProgramDate", and "ProgramTitle". code show as below:

Class TVProgram

Public StartTime

Public ProgramDate

Public ProgramTITE

END CLASS

Dim objtvshow

Set objtvshow = new TVprogram

Objtvshow.starttime = cdate ("

17:30

")

Objtvshow.programdate = dateserial (1999, 9, 17)

Objtvshow.programtitle = "The Jerry Springer Show"

Response.write objtvshow.programtitle & "IS on AT" & _

Objtvshow.startTime & "On" & objtvshow.programdate

The way the code work is, we define the properties of StartTime, ProgramDate, and ProgramTitle as class TVProgram. Thus, these attributes are processed like other variables, and the code is not executed without setting values. The public field before the property name has its true meaning and is very important. If you don't specifically refer to a method or attribute as public or private, the system default is public, but it is best to develop a good writing habit of defining any value (which is convenient for you to read).

The result of the above program is approximately as follows (decided on your local server configuration):

The Jerry Springer Show is on At

5:30 pm

on

17/09/99

.

I am in the UK, so the date reality is as above. No matter what project you run, it's good, but only you start using other objects, create a perfect interface for all the information and features you may need, to support the entities surrounded by the object you build, you I will experience the real power of the object.

Now, if you don't like the above example display date, you want to use the same format realistic date, don't add formatDateTime () when you reference each ProgramDate property, and you only need to implant the property itself. This requires another method to define attributes. Similarly, we will use programdate as an external visible attribute, but because the programdate property will become a function instead of static value, we save the actual date in another attribute in INTERNAL_PROGRAMDATE.

Class TVProgram

Public StartTime

Publicinal_ProgramDate

Public property Get Programdate, PROGRAMDATE

Programdate = day (internal_programdate) & _

"" & Monthname (Month (Internal_ProgramDate) &_

"" & Year (internal_programdate)

End Property

Public ProgramTITE

END CLASS

Dim objtvshow

Set objtvshow = new TVprogram

Objtvshow.starttime = cdate ("

17:30

")

Objtvshow.internal_ProgramDate = DateSerial (1999, 9, 17)

Objtvshow.programtitle = "The Jerry Springer Show"

Response.write objtvshow.programtitle & "IS on AT" & _

Objtvshow.startTime & "On" & objtvshow.programdate & "."

The results of the program are as follows:

The Jerry Springer Show is on At

5:30 pm

on

17 September 1999

.

转载请注明原文地址:https://www.9cbs.com/read-12994.html

New Post(0)