手机
当前位置:查字典教程网 >编程开发 >ASP教程 >使用类,实现模块化
使用类,实现模块化
摘要:写在前面的话:所有写程序的人都知道,当你逐渐对您要实现的功能展开的时候,很大的时候,第一天写的东西第二天就忘了写到那里了,很多的时候,不得不...

写在前面的话:

所有写程序的人都知道,当你逐渐对您要实现的功能展开的时候,很大的时候,第一天写的东西第二天就忘了写到那里了,很多的时候,不得不写上详细的程序开发笔记,这在ASP的系统开发中感觉尤其文件、函数复杂的时候,当我们打算对网站的一部分功能进行修改的时候,感觉无从下手或者感觉要修改的地方。这时候,如果您学过任何一门面向对象的编程的语言的话,自然想到怎么能把代码功能实现模块话,asp本质上不是面向对象的编程,但VBSCRPIT6.0提供了类,我们可以通过类实现代码的封装,实现模块话。

首先,我要在这里写上一些很官方的概念,意在说明面向对象是很具体化的,很实体的模式,不能让有些人看见“对象”就被吓跑了。

对象,就是能看到,感到,听到,触摸到,尝到或闻到的东西,在这里我们这样“定义”:对象是一个自包含的实体,用一组可识别的特性和行为来标识。

在面向对象的编程(oop)的编程方式,用使用下面的两个术语。

类:这是对象的模板,定义了对象的特性。

实例:这是一个真实的对象,可以与之交互的东西。

属性,方法和事件

在OOP中,下面的术语描述对象的特性:

属性:这是一个名次,描述了某个对象的属性。

方法:这是一个动词,描述了对象可以完成的工作,或者希望它完成的工作。

事件:描述了对象为相应某个动作而执行的操作。

在编程时,对象的面向对象编程和面向对象设计的一部分,它们具有非常大的优势,许多人认为这是一个复杂的主题,但实际上,它非常简单,可以用四个简单的术语来解释:抽象、封装、多态和继承。

抽象:这是一个隐藏复杂性,类的内部工作情况,所以用户不必知道它的运作方式,就像。如果想要看电视,就不必知道电视机时如何工作的,只需打开电视机,搜索频道即可,on/off开关抽象了实际的操作,在string例子里,有一个trim方法,它可以删除字符串尾部的空格,同样不需要知道他是如何完成这个任务的,只要知道它有这个功能即可。

封装:每个对象都包含进行操作所需要的所有信息,这个对象称为封装,因此对象不比依赖其他对象来完成自己的操作,在术语TOupper()方法中,string不必到其他地方获取信息来把所有的字符转换为大写。

多态:这个术语用于表示不同的对象可以执行相同的动作,但要通过他们自己的实现代码来执行,名称一样,但底层实现的代码是不一样的。

继承:它定义了类如何相互关联,共享特性的,继承的工作方式是,定义类和子类,其中子类继承了父类的所有特性,继承的重要性是,它迫使类型相似的类具有一致性,并允许共享代码,如果决定创建一个新类,就不必定义父类的所有特性。

在ASP中使用类,实现模块化

下面我通过举上几个简单的例子说明一下,注意,这里强调的是一种思想,如果在您开发ASP网站的时候能用一个类(基类)展开的话,这是很有必要的(也是很有难度的)。

我们先选择一个简单的例子:

我们要显示经典论坛用户的信息,当输入用户的ID以后能,显示出该用户的一些信息,这是一个过程,可以这样考虑,我们把用户当作一个对象,他有的属性是ID,性别,积分,权限,实现的方法有显示这些信息,ok,这样写:

Classblueidea

Privatebname,bpoint,bsex,blevel

'...................

endclass

这里先声明了一个名为blueidea的类,接着是一些私有变量,用于存储blueidea类的属性,这些变量在代码的外部不能访问,这就是数据保护,要定义这些变量,使用了property语句获得值间接的付给私有变量

'-----------------------------------------------------------------

PropertyGetgetname

getname=bname

EndProperty

PropertyLetgetname(nameid)

bname=nameid

Ifnameid=""Then

bname="没注册用户"

EndIf

EndProperty

'------------------------------------------------------------------

PropertyGetgetsex

getsex=bsex

EndProperty

PropertyLetgetsex(sex)

bsex=killint(sex,0,0)

Ifbsex=0Then

bsex="男"

Else

bsex="女"

Endif

EndProperty

'------------------------------------------------------------------

PropertyGetgetpoint

getpoint=bpoint

EndProperty

PropertyLetgetpoint(point)

bpoint=killint(point,0,0)

EndProperty

'------------------------------------------------------------------

这里有个killint函数,是判断数据合法性的,它的原形是:

PrivateFunctionkillint(i,killstr,killsub)

IfNotIsNumeric(i)Then

i=killstr

ElseIfi<=0Then

i=killsub

Endif

killint=Int(Left(i,5))

EndFunction

该函数功能很明确,不再繁琐说。

由于我们要通过积分判断用户级别,这里定义了一个私有函数:

PrivateFunctiongetlevel()

bpoint=killint(bpoint,0,0)

Ifbpoint<500Then

blevel="初级会员"

ElseIfbpoint>=500Andbpoint<=100Then

blevel="高级会员"

Else

blevel="终极会员"

EndIf

Getlevel=blevel

EndFunction

我们要得是回送用户的信息,必须定义一个public公用函数,显示信息:

PublicFunctionshowuser()

response.write("<h5>以下显示<fontcolor=red>"&bname&"</font>的资料:</h5>")

response.write("<h5>性别:<fontcolor=red>"&bsex&"</font></h5>")

response.write("<h5>积分:<fontcolor=red>"&bpoint&"</font></h5>")

getlevel

response.write("<h5>级别:<fontcolor=red>"&blevel&"</font></h5>")

EndFunction

Endclass

使用这个类的时候这样使用:(我在这里写了一个表单处理的)

Setblueideauser=newblueidea

blueideauser.getname=Trim(request("id"))

blueideauser.getsex=request("sex")

blueideauser.getpoint=request("point")

blueideauser.showuser

是不是想看看效果,那就看看这里:http://www.5do8.com/net/aspclass/class.asp

控制读取数据库信息的类:

参考源码

'名称:ado_5do8

'作用:读取数据库的各项操作

'来源-耕耘村http://www.5do8.comhttp://www.Blueidea.com-5do8

'创作:5do8

'联系:5do8@5do8.com

'更新:2005年11月13日

'授权:蓝色理想网站积分超过3000,耕耘村所有注册用户

'类的接口:ado_5do8.ConnectString=数据库绝对路径

'ado_5do8.rs_top调用数目,表的名称

Classado_5do8

Privateconn,sqlstr,rs,iid,itable,isession

'sqlstr:数据库地址,为绝对路径,私有

'conn:打开数据库的连接,私有

'------------------------------------------------------------------

rem消除一些不想要的数字

PrivateFunctionlitter_in(r1,r2)

IfIsNumeric(r1)andIsNumeric(r2)Then

Dimdimrr

Ifr1>r2Then

dimrr=r2

Else

dimrr=r1

EndIf

Else

dimrr=0

Endif

litter_in=dimrr

EndFunction

'-----------------------------------------------------------------

PrivateFunctionkillint(i,killstr,killsub)

IfNotIsNumeric(i)Then

i=killstr

ElseIfi<=0Then

i=killsub

Endif

killint=Int(Left(i,5))

EndFunction

'-----------------------------------------------------------

privateSubstartconn()

OnErrorResumeNext

Setconn=server.CreateObject("adodb.connection")

strconn="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="&Server.MapPath(sqlstr)

conn.openstrconn

IfErrThen

err.Clear

SetConn=Nothing

mess="发生错误,不能连接数据库"

response.write(mess)

response.End

Else

mess="连接数据库conn成功...........<br/>"

response.write(mess)

EndIf

EndSub

'----------------------------------------------------------------

privateSubcloseconn()

conn.close

Setconn=Nothing

response.write("<strongstyle='color:red'>关闭conn连接</strong>...<hr/>")

Endsub

'-----------------------------------------------------------------

PrivateSubclosers()

rs.close

Setrs=Nothing

response.write("<strongstyle='color:#085420'>关闭数据库RS</strong>.......<br/>")

EndSub

'-----------------------------------------------------------------

PropertyGethavese

havese=isession

EndProperty

PropertyLethavese(yoursession)

isession=yoursession

Ifyoursession=""Then

isession="nodef"

EndIf

EndProperty

'-----------------------------------------------------------------

PublicFunctionmakesession(arraydata)

IfIsArray(arraydata)then

makear=arraydata

Else

makear=Array(0,0,0,0)

EndIf

Ifisession=""Then

isession="nodef"

Endif

session(isession)=makear

EndFunction

'-----------------------------------------------------------------

privateFunctiongetsession()

thisget=session(isession)

IfNotIsArray(thisget)Then

thisget=Array(0,0,0,0)

EndIf

Getsession=thisget

Endfunction

'-----------------------------------------------------------------

PropertyGetConnectString

ConnectString=sqlstr

EndProperty

PropertyLetConnectString(str)

sqlstr=str

EndProperty

'-----------------------------------------------------------------

PropertyGetgetid

getid=iid

EndProperty

PropertyLetgetid(id)

iid=id

EndProperty

'-----------------------------------------------------------------

PropertyGetgettable

gettable=itable

EndProperty

PropertyLetgettable(table)

itable=table

EndProperty

'-----------------------------------------------------------------

'------------------------------------------------------------------

publicFunctionreadarraysession(iStart,ipageno,irowid)

rowid=killint(irowid,0,0)

start=killint(istart,0,0)

pageno=killint(ipageno,5,5)

data=getsession

iRows=UBound(data,2)

iCols=UBound(data,1)

response.write("<h5>总数获得了:")

response.write("<b>"&iRows+1&"</b>条信息</h5><hr/><ulstyle='width:100%;'>")

Ifrowid=0then

IfiRows>(ipageno+iStart)Then

iStop=ipageno+iStart-1

Else

iStop=iRows

EndIf

ForiRowLoop=StarttoiStop

Response.Write("<listyle='padding:4px0;'><ahref=?k=read&rowid="&irowloop+1&">"&data(1,iRowLoop)&"</a><spanstyle='padding:4px04px10px;background-color:#ccc;'>较慢,不推荐点击--><ahref=?k=list&id="&data(0,irowloop)&">更新</a></span></li>")

Next

Response.Write"</ul><divstyle='top:20px;background-color:#ccc;color:#020;font-weight:bold;bordr-top:2pxsolid#008;padding:10px0;color:#b00'>列表(<ahref=default.asp>回到典型模式</a>):"

ifStart>0then

Response.Write"<AHREF=""?k=read&Start="&iStart-ipageno&"&pageno="&ipageno&""">Previous</A>"

endif

ifiStop<iRowsthen

Response.Write"<AHREF=""?k=read&Start="&iStart+ipageno&"&pageno="&ipageno&""">Next</A>"

endIf

response.write"</div>"

Else

rowid=litter_in(rowid-1,iRows)

response.write("<divstyle='width:85%'><h4style='text-align:center'><ahref=?k=read&pageno="&pageno&"&start="&start&">返回列表</a></h4></h2><hr/><h5>"&server.htmlencode(data(1,rowid))&"</h5><p>"&server.htmlencode(data(2,rowid))&"<h5>+-----"&server.htmlencode(data(3,rowid))&"")

response.write("<div>")

Endif

EndFunction

'-----------------------------------------------------------------

PublicFunctionlist_ids()

sql3="select*from"&itable&"whereid="&iid&""

startconn()

Setrs=conn.execute(sql3)

Ifrs.eofAndrs.bofThen

data=Array(0,0,0,0)

Else

data=Rs.GetRows()

EndIf

closers

closeconn

response.write(UBound(data)&":")

response.write(server.htmlencode(data(2,0)))

Endfunction

'-----------------------------------------------------------------

PublicFunctionrs_top(num,table,whe)

startconn()

sql="selecttop"&num&"*from"&table&""

sql2="selectcount(*)asszd_countfrom"&table&""""&whe&""

Setrs=conn.execute(sql2)

szd_count=rs("szd_count")

closers

Setrs=Conn.Execute(sql)

dimdata

IfRs.EofThen

data="nodata"

Else

data=Rs.GetRows()

Endif

closers

closeconn()

Callmakesession(data)

EndFunction

'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

EndClass

使用的时候:

Dimaction

action=request("k")

Ifaction="view"Then

Callviewnew

ElseIfaction="list"Then

Calllist()

ElseIfaction="read"Then

Callread()

Else

Callff()

Endif

Subff()

%>

<formstyle="border-top:2pxsolid#008;border-bottom:2pxsolid#008;margin:auto;background-color:#eee;padding:20px5px;color:#008;font-weight:bold;">

<label>显示信息总数:<inputname="n"type="text"maxlength="4"size="10"/>每页数目:<inputname="pagesize"type="text"maxlength="4"size="10"value="5"/><inputname="arrstart"type="hidden"value="0"></label>

<h5style="border-top:1pxsolid#000;padding:5px0">操作:<inputname="k"type="submit"value="view"/></h5>

</form><%Endsub%>

<%Subviewnew()

f_num=killint(request("n"),1,1)

pagesize=killint(request("pageno"),5,5)

arrstart=killint(request("start"),0,0)

rowid=killint(request("rowid"),0,0)

Setcs=newado_5do8

cs.ConnectString="data/a.mdb"

cs.havese="shi"

cs.rs_topf_num,"site_szd",""

cs.readarraysessionarrstart,pagesize,rowid

Endsub

Sublist()

response.write("<h5><ahref=default.asp>返回默认模式</a></h5>")

response.write"下面显示具体信息:<hr/>"

id=request("id")

id=killint(id,1,1)

Setlistid=newado_5do8

listid.ConnectString="data/a.mdb"

listid.getid=id

listid.gettable="site_szd"

listid.list_ids()

EndSub

Subread()

response.write"<divstyle='background-color:#ccc;padding:20px0;color:080;font-weight:bold;border-bottom:2pxsolid#008'>页面分析完毕,要更新请选择<ahref=default.asp>回到典型模式</a>参数:Start,开始元素;pageno,每页条数</div>"

pagesize=killint(request("pageno"),5,5)

arrstart=killint(request("start"),0,0)

rowid=killint(request("rowid"),0,0)

Setcs=newado_5do8

cs.havese="shi"

cs.readarraysessionarrstart,pagesize,rowid

Endsub

Functionkillint(i,killstr,killsub)

IfNotIsNumeric(i)Then

i=killstr

ElseIfi<=0Then

i=killsub

Endif

killint=Int(Left(i,5))

EndFunction

%>

演示在:http://www.5do8.com/net/aspclass

【使用类,实现模块化】相关文章:

ASP无组件分页实现思路及代码

ASP中七种实现分页显示的方法

PJBLOG使用技巧

ASP使用MYSQL数据库全攻略

用ASP+DLL实现WEB方式修改服务器时间

用ASP+CSS实现随机背景

用XML+FSO+JS实现服务器端文件的

ASP怎么谈到应用到类的?

利用ASP实现事务处理的方法

asp获取用户真实IP地址的方法

精品推荐
分类导航