忙著找工作,直到周末才有空。現在來繼續我們的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 %>
注意賦日期的用法,生成的控件的名字,可以查看源代碼。還有一個CSS的測試,就留給朋友們自己去做吧。
|