使用类,实现模块化

发布时间:2022-04-17 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用类,实现模块化脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

写在前面的话:

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

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

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

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

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

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

  属性,方法和事件

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Property Let getname(nameid)
bname=nameid
If nameid="" Then
bname="没注册用户"
End If
End Property
'------------------------------------------------------------------ 
Property Get getsex
getsex=bsex
End Property

Property Let getsex(sex)
bsex=killint(sex,0,0)
If bsex=0 Then
bsex="男"
Else
bsex="女"
End if
End Property
'------------------------------------------------------------------ 
Property Get getpoint
getpoint=bpoint
End Property

Property Let getpoint(point)
bpoint=killint(point,0,0)
End Property
'------------------------------------------------------------------

  这里有个killint函数,是判断数据合法性的,它的原形是:
Private Function killint(i,killstr,killsub)
If Not IsNumeric(i) Then
i=killstr
ElseIf i<=0 Then
i=killsub
End if
killint=Int(Left(i,5))
End Function

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

由于我们要通过积分判断用户级别,这里定义了一个私有函数:
Private Function getlevel()
bpoint=killint(bpoint,0,0)
If bpoint<500 Then
blevel="初级会员"
ElseIf bpoint>=500 And bpoint<=100 Then
blevel="高级会员"
Else
blevel="终极会员"
End If
Getlevel=blevel
End Function

  我们要得是回送用户的信息,必须定义一个public公用函数,显示信息:
Public Function showuser()
response.write("<h5>以下显示<font color=red>"&bname&"</font>的资料:</h5>")
response.write("<h5>性别:<font color=red>"&bsex&"</font></h5>")
response.write("<h5>积分:<font color=red>"&bpoint&"</font></h5>")
getlevel
response.write("<h5>级别:<font color=red>"&blevel&"</font></h5>")
End Function
End class

  使用这个类的时候这样使用:(我在这里写了一个表单处理的)
Set blueideauser=new blueidea
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.com http://www.Blueidea.com-5do8
'创作:5do8
'联系:5do8@5do8.com
'更新:2005年11月13日
'授权:蓝色理想网站积分超过3000,耕耘村所有注册用户
'类的接口:ado_5do8.ConnectString=数据库绝对路径
'ado_5do8.rs_top 调用数目,表的名称 
Class ado_5do8
Private conn,SQLstr,rs,iid,itable,isession
'sqlstr:数据库地址,为绝对路径,私有
'conn:打开数据库的连接,私有

'------------------------------------------------------------------
rem 消除一些不想要的数字 
Private Function litter_in(r1,r2)
If IsNumeric(r1) and IsNumeric(r2) Then
Dim dimrr
If r1>r2 Then
dimrr=r2
Else
dimrr=r1
End If
Else
dimrr=0
End if
litter_in=dimrr
End Function
'-----------------------------------------------------------------
Private Function killint(i,killstr,killsub)
If Not IsNumeric(i) Then
i=killstr
ElseIf i<=0 Then
i=killsub
End if
killint=Int(Left(i,5))
End Function
'-----------------------------------------------------------
private Sub startconn()
On Error Resume Next 
Set conn=server.CreateObject("adodb.connection")
strconn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(sqlstr)
conn.open strconn
If Err Then
err.Clear
Set Conn = Nothing
mess="发生错误,不能连接数据库"
response.write(mess)
response.End
Else
mess="连接数据库conn成功...........<br/>"
response.write(mess)
End If
End Sub
'----------------------------------------------------------------
private Sub closeconn()
conn.close 
Set conn=Nothing
response.write("<strong style='color:red'>关闭conn连接</strong>...<hr/>")
End sub 
'-----------------------------------------------------------------
Private Sub closers()
rs.close
Set rs=Nothing
response.write("<strong style='color:#085420'>关闭数据库RS</strong>.......<br/>")

End Sub

'-----------------------------------------------------------------
Property Get havese
havese=isession
End Property

Property Let havese(yoursession)
isession=yoursession
If yoursession="" Then
isession="nodef"
End If
End Property

'-----------------------------------------------------------------
Public Function ;makesession(arraydata)
If IsArray(arraydata) then
makear=arraydata
Else
makear=Array(0,0,0,0)
End If
If isession="" Then
isession="nodef"
End if
session(isession)=makear
End Function
'-----------------------------------------------------------------

private Function getsession()
thisget=session(isession)
If Not IsArray(thisget) Then
thisget=Array(0,0,0,0)
End If
Getsession=thisget
End function
'-----------------------------------------------------------------
Property Get ConnectString 
ConnectString = sqlstr
End Property
Property Let ConnectString(str) 
sqlstr = str
End Property
'-----------------------------------------------------------------

Property Get getid 
getid = iid
End Property
Property Let getid(id) 
iid = id
End Property
'-----------------------------------------------------------------

Property Get gettable 
gettable = itable
End Property
Property Let gettable(table) 
itable = table
End Property
'-----------------------------------------------------------------
'------------------------------------------------------------------
public Function readarraysession(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/><ul style='width:100%;'>")
If rowid = 0 then
If iRows > (ipageno + iStart) Then
iStop = ipageno + iStart - 1
Else
iStop = iRows
End If
For iRowLoop = Start to iStop
Response.Write ("<li style='padding:4px 0;'><a href=?k=read&rowid="&irowloop+1&">"&data(1, iRowLoop) & " </a><span style='padding:4px 0 4px 10px;background-color:#ccc; '>较慢,不推荐点击--><a href=?k=list&id="&data(0,irowloop)&">更新</a></span></li>")
Next 
Response.Write "</ul><div style='top:20px;background-color:#ccc;color:#020;font-weight:bold;bordr-top:2px solid #008;padding:10px 0;color:#b00'>列表(<a href=default.asp>回到典型模式</a>):"
if Start > 0 then
Response.Write "<A HREF=""?k=read&Start=" & iStart-ipageno &"&pageno=" & ipageno & """>Previous</A>"
end if 
if iStop < iRows then
Response.Write " <A HREF=""?k=read&Start=" & iStart+ipageno &"&pageno=" & ipageno & """>Next</A>"
end If

response.write"</div>"

Else
rowid=litter_in(rowid-1,iRows)
response.write("<div style='width:85%'><h4 style='text-align:center'><a href=?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 >")
End if
End Function

'-----------------------------------------------------------------
Public Function list_ids()
sql3="select * From "&itable&" where id="&iid&" "
startconn()
Set rs=conn.execute(sql3)
If rs.eof And rs.bof Then
data=Array(0,0,0,0)
Else
data=Rs.GetRows()
End If
closers
closeconn
response.write(UBound(data)&":")
response.write(server.htmlencode(data(2,0)))
End function

'-----------------------------------------------------------------
Public Function rs_top(num,table,whe)
startconn()
sql="select top "&num&" * from "&table&""
sql2="select count(*) as szd_count from "&table&" "" "&whe&""
Set rs=conn.execute(sql2)
szd_count=rs("szd_count")
closers
Set rs = Conn.Execute(sql)
dim data
If Rs.Eof Then
data="no data"
Else
data=Rs.GetRows()
End if
closers
closeconn()
Call makesession (data)
End Function
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
End Class

  使用的时候:
Dim action 
action=request("k")
If action="view" Then
Call viewnew
ElseIf action="list" Then
Call list()
ElseIf action="read" Then
Call read()
Else
Call ff()
End if
Sub ff()
%> 
<form style="border-top:2px solid #008;border-bottom:2px solid #008;margin:auto;background-color:#eee;padding:20px 5px;color:#008;font-weight:bold;"> 
<label>显示信息总数:<input name="n" type="text" maxlength="4" size="10" />每页数目:<input name="pagesize" type="text" maxlength="4" size="10" value="5"/><input name="arrstart" type="hidden" value="0"></label>

<h5 style="border-top:1px solid #000;padding:5px 0"> 操作:<input name="k" type="submit" value="view" /></h5>

</form> <%End sub%>
<%Sub viewnew()
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)
Set cs=new ado_5do8
cs.ConnectString="data/a.mdb"
cs.havese="shi"
cs.rs_top f_num,"site_szd",""
cs.readarraysession arrstart,pagesize,rowid
End sub
Sub list()
response.write("<h5><a href=default.asp>返回默认模式</a></h5>")
response.write"下面显示具体信息:<hr/>"
id=request("id")
id=killint(id,1,1)
Set listid=new ado_5do8
listid.ConnectString="data/a.mdb"
listid.getid=id
listid.gettable="site_szd"
listid.list_ids()
End Sub

Sub read()
response.write"<div style='background-color:#ccc;padding:20px 0;color:080;font-weight:bold;border-bottom:2px solid #008'>页面分析完毕,要更新请选择<a href=default.asp>回到典型模式</a>参数:Start,开始元素;pageno,每页条数</div>"
pagesize=killint(request("pageno"),5,5)
arrstart=killint(request("start"),0,0)
rowid=killint(request("rowid"),0,0)
Set cs=new ado_5do8
cs.havese="shi"
cs.readarraysession arrstart,pagesize,rowid

End sub

Function killint(i,killstr,killsub)
If Not IsNumeric(i) Then
i=killstr
ElseIf i<=0 Then
i=killsub
End if
killint=Int(Left(i,5))
End Function 
%>

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

@H_777_393@

脚本宝典总结

以上是脚本宝典为你收集整理的使用类,实现模块化全部内容,希望文章能够帮你解决使用类,实现模块化所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: