VBscript 類的設計

        應miles的要求,同時也作爲類設計的一個例子,我把我設計日期類的過程整理出來,寫成這篇文章,供大家做個參考。也希望這篇文章能拋磚引玉,讓大家寫出更好,更多的類來。文中有不盡的地方,還請指正。
(一)VBscript自定義類
         簡單的說,類就是對象,它具有屬性和方法。在vbscript裏自定義類比C++,JAVA要簡單得多。下面將設計一個日期類,用來顯示出組合的表單對象。在設計的同時,我們也會說明如何設計自定義類。
1、定義類的語法:class....end class
class及end class用來標識類塊的開始和結束,class 標識符後面跟着的是類名稱。現在我們把要設計的日期類命名爲:dateclass 語法如下:
class dateclass
...
end class

在vbscript中使用些類建立新對象時,可以用new運算符。例如:
set newdate=new dateclass

2、屬性和方法:private、public和property
private 用來定義僅能在類內部訪問的變量和函數;public則用來定義類的界面,也就是可供外部訪問的屬性和方法,沒有成爲 private和public的成員,一律視爲public;有和時候,我們要讓外部可以訪問,但是要指定訪問的方法,這時候,就要用property,property語法如下:
public property [let|set|get] aa(...)
...
end property

property 必須和 let、set或get 配合使用。說明如下:
let 設置值,如:user.age=100
set 設置對象,如:set user.myobject=nobject
get 取得值,如:myage=user.age

3、設計日期類的屬性和方法
現在我們來設計日期類的屬性。爲了顯示日期,定義一個classdate,來存放日期,它的類型是public,這樣可以讓用戶在類外部改變它;再設計一個函數來顯示日期,取名爲:datedisplay,類型爲public,沒有參數。程序如下:
<%
class dateclass

public classdate

public function datedisplay()

end function

end class
%>

4、加入顯示日期的代碼
現在我們來加入datedisplay的代碼,程序如下:
<%
class dateclass

public classdate

public function datedisplay()
'如果沒有指定日期,那麼設爲當前日期
if classdate="" then
classdate=now()
end if
yy=year(classdate)
mm=month(classdate)
dd=day(classdate)
response.write "<input type=text name=yy size=4 maxlength=4 value="&yy&">年"&vbcrlf
response.write "<select name=mm>"&vbcrlf
for i=1 to 12
if i=mm then
response.write "<option value="&i&" selected>"&i&"</option>"&vbcrlf
else
response.write "<option value="&i&">"&i&"</option>"&vbcrlf
end if
next
response.write "</select>月"&vbcrlf
response.write "<select name=dd>"&vbcrlf
for i=1 to 31
if i=dd then
response.write "<option value="&i&" selected>"&i&"</option>"&vbcrlf
else
response.write "<option value="&i&">"&i&"</option>"&vbcrlf
end if
next
response.write "</select>日"&vbcrlf
end function

end class
%>

把上面的代碼存爲dateclass1.asp 好了,現在我們已經寫好了這個日期類,下面來看看使用的情況
5、調用類 include
在其它程序中使用這個類,只要用include 把dateclass1.asp包含進來。下面我們寫一個測試的程序,文件名爲 test1.asp
<!--#include file="dateclass1.asp" -->
<%
set newdate= new dateclass
response.write "調用顯示:<br>"
newdate.datedisplay
response.write "<br>顯示classdate的值:<br>"
response.write newdate.classdate
response.write "<br>設置classdate的值:<br>"
newdate.classdate=cdate("2005/6/15")
'上一句也可以寫成:
'newdate.classdate="2005/6/15"
response.write "<br>再調用顯示:<br>"
newdate.datedisplay
set newdate=nothing
%>

把兩個文件放在同一個目錄下,運行test1.asp 好了,應該已經看到結果了。但是這樣的設計還有一些問題:
1、如果用戶指定的classdate不是日期型,那麼日期就會變成1900年1月1日;
2、如果顯示多個日期,表單對象的名字不能是一樣的;
3、最好加入CSS;
4、最好還能加入閏年的判斷;
5、不是每個月都有31天;
帶着這些問題,我們將會繼續........

 

忙着找工作,直到週末纔有空。現在來繼續我們的vbscript類的設計。上一次我們已經設計出了一個簡單的日期類,但是還存在一些使用

上的問題。現在我們針對這些問題來更改我們的設計。這次我們要解決的問題是:
1、如果用戶指定的classdate不是日期型,那麼日期就會變成1900年1月1日;
2、如果顯示多個日期,表單對象的名字不能是一樣的;
3、最好加入CSS;
下面分別討論
一、在上一次我們設計的dataclass 裏,我們定義了一個public型的變量 classdate 來表示日期,用戶可以自由地對classdate 進行值和

讀取,這樣就無法判斷用戶所賦的值是不是正確的了。現在我們把重定義一個變量 cldate 改成 private 型,這樣用戶就無法直接訪問cldate

了。如下:
private cldate

爲了能夠讓用戶訪問到classdate,上一次提到了 property ,現在我們就用它來構造二個函數,一個是讓用戶對classdate

賦值的函數,叫classdate。定義如下:
pbulic property let classdate(fdate)
...
end property

另一個函數是讓用戶讀取cldate,定義如下:
public property get classdate()
...
end property

好,定義好函數後,就可以來寫代碼了,在 let classdate 裏,主要是判斷用戶輸入的是不是日期型,如下:
public property let classdate(fdate)
if typename(fdate)="Date" then
cldate=fdate
end if
end property

在 get classdate 裏,主要是把cldate 的值賦給函數:如下:
public property get classdate()
classdate=cldate
end property

 

如果沒有給clname賦值呢?也就是當這個類剛生成的時候,clname是個空值,這裏介紹一下類初始化和終止時的自動進程:clas_initialize

和 class_terminate。第一個進程是生成類裏自動執行的,第二個是類消滅時自動執行的。這裏只要用到第一個進程。如下:
private sub class_initialize()
cldate=now()
end private

這樣,類生成的時候,如果沒有對clname賦值,那麼clname就是當前的日期。

二、爲了在表單裏顯示出多個日期,我們要區分每次生成的日期對象,也就是取個名字。在這裏我們定義一個private型的變量 clname 來

定義生成的表單控件的名字。定義如下:
private clname

同樣的,要讓用戶能訪問到clname變量,也要定義兩個函數,方法和classdate的相同。代碼如下:
public property let classname(fname)
if fname<>"" then
clname=fname
end if
end property

public property get classname()
classname=clname
end property

在每次顯示錶單日期控件的時候,要給控件加上名字,我們分別將年、月、日命名爲:
clname & "_year"
clname & "_month"
clname & "_day"
同樣的,我們讓clname默認爲"datename",也就是在class_initialize里加上代碼,改進後的class_initialize如下:
private sub class_initialize()
cldate=now()
clname="datename"
end private

爲了讓每次顯示日期後clname的值不同,我們在clname後面加上數字來區別。這裏我們引入一個private變量clcount,生成類時,clcount

的值爲“0”,如果對clname賦值,那麼clcount又置爲0代碼如下:
private clcount

private sub class_initialize()
cldate=now()
clname="datename"
clcount=0
end sub

public property let classname(fname)
if fname<>"" then
clname=fname
clcount=0
end if
end property

三、最後,我們引入一個private變量clcss來定義表單控件的CSS。只要提供賦值的函數就可以了,讀取CSS在這裏沒什麼用。代碼如下:
private clcss

public property let classcss(fcss)
if fcss<>"" then
clcss=fcss
end if
end property

四、好,現在要來修改顯示日期函數了,每次顯示後讓clcount的值加1。修改後的文件保存爲dateclass2.asp,完整代碼如下:
<%
class dateclass

'定義變量
private cldate
private clname
private clcount
private clcss

'初始化
private sub class_initialize()
cldate=date()
clname="datename"
clcount=0
end sub

'訪問cldate
public property let classdate(fdate)
if typename(fdate)="Date" then
cldate=fdate
end if
end property

public property get classdate()
classdate=cldate
end property

'訪問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

'訪問clcss
public property let classcss(fcss)
if fcss<>"" then
clcss=fcss
end if
end property

'修改後的顯示日期函數
public function datedisplay()
'給顯示的表單控件自動編號
if clcount>0 then
cname=clname & cstr(clcount-1)
else
cname=clname
end if
yy=year(classdate)
mm=month(classdate)
dd=day(classdate)
response.write "<input type='text' name='" & cname & "_year' size='4'" &_
" class='" & clcss & "' maxlength='4' value='" &_
yy & "'>年" & vbcrlf
response.write "<select class='" & clcss & "' name='" &_
cname &"_month'>" & vbcrlf
for i=1 to 12
if i=mm then
response.write "<option value='"&i&"' selected>"&i&"</option>" & vbcrlf
else
response.write "<option value='"&i&"'>"&i&"</option>" & vbcrlf
end if
next
response.write "</select>月" & vbcrlf
response.write "<select class='" & clcss & "' name='" & cname & "_day'>" & vbcrlf
for i=1 to 31
if i=dd then
response.write "<option value='"&i&"' selected>"&i&"</option>" & vbcrlf
else
response.write "<option value='"&i&"'>"&i&"</option>" & vbcrlf
end if
next
response.write "</select>日" & vbcrlf

clcount=clcount+1
end function

end class
%>

五、調試。只要把上一次的test1.asp稍加修改就可以了,如下:
<!--#include file="dateclass2.asp" -->
<%
br="<br>"
set newdate= new dateclass
response.write "調用顯示:" & br
response.write "生成的控件名是:"& newdate.classname &br
newdate.datedisplay
response.write br
newdate.datedisplay
response.write br & "顯示classdate的值:"
response.write newdate.classdate
response.write br & "設置classdate的值爲“2005/6/15”:" & br
newdate.classdate=cdate("2005/6/15")
response.write br & "設置名字爲“mydate”:" & br
newdate.classname="mydate"
response.write "再調用顯示:" & br
response.write br & "生成的控件名是:"& newdate.classname &br
newdate.datedisplay
response.write br & "生成控件:"&br
newdate.datedisplay
set newdate=nothing
%>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章