VBscript 类的设计(二) - 中国WEB开发者网络 (http://www.webasp.net) -- 技术教程 (http://www.webasp.net/article/) --- VBscript 类的设计(二) (http://www.webasp.net/article/9/8173.htm) |
| -- 作者:未知 -- 发布日期: 2004-03-10 |
| 忙着找工作,直到周末才有空。现在来继续我们的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的测试,就留给朋友们自己去做吧。 |
| webasp.net |