Written in front of the words:
Everyone who writes a program knows that when you gradually expand the functionality you are about to implement, very big time, the first day of writing things forgot to write there, a lot of time, have to write detailed program development notes, this in the ASP's system development, especially when the file, function complex, When we intend to modify some of the functions of the site, feel no way or feel to change the place. At this time, if you have learned any object-oriented programming language, naturally think of how to implement the Code function module, ASP is not in essence object-oriented programming, but VBSCRPIT6.0 provides classes, we can implement the code through the encapsulation, implementation module words.
First of all, I want to write some very official concepts here to illustrate that object-oriented is very concrete, very solid mode, not to let some people see "object" is scared away.
Objects are things you can see, feel, hear, touch, taste, or smell, and here we are. "Definition": an object is a self-contained entity that is identified by a set of identifiable attributes and behaviors.
In object-oriented programming (OOP), use the following two terms.
Class: This is the template for the object, which defines the attributes of the object.
Example: This is a real object that can interact with it.
Properties, methods, and events
In OOP, the following terms describe the attributes of an object:
Attribute: This is a ranking that describes the properties of an object.
Method: This is a verb that describes the work that an object can accomplish, or the work it is expected to accomplish.
Events: Describes the actions that an object performs for a corresponding action.
In programming, objects are part of object-oriented programming and object-oriented design, they have great advantages, many people think this is a complex topic, but in fact, it is very simple, can be explained in four simple terms: abstraction, encapsulation, polymorphism and inheritance.
Abstract: This is a hidden complexity, the internal workings of the class, so the user does not have to know how it works, just like. If you want to watch TV, you don't have to know how to work the TV, just turn on the TV, search the channel, the On/off switch abstracts the actual operation, in the string example, there is a trim method, it can delete the string trailing spaces, also do not need to know how he completed the task, Just know that it has this function.
Encapsulation: Each object contains all the information that is required for the operation, which is called encapsulation, so that the object does not perform its own operation more than relying on other objects, and in the term ToUpper () method, a string does not have to get information elsewhere to convert all characters to uppercase.
Polymorphism: This term is used to indicate that different objects can perform the same actions, but by their own implementation code to execute, the name is the same, but the underlying implementation of the code is not the same.
Inheritance: It defines how classes relate to each other, share attributes, inheritance works by defining classes and subclasses in which subclasses inherit all the attributes of the parent class, and the importance of inheritance is that it forces a class of similar types to be consistent and allows sharing of code, and if you decide to create a new class, you do not have to define all the attributes of the parent class.
Using classes in ASP to achieve modularity
Let me take a few simple examples to illustrate, note that here is an emphasis on the idea that it is necessary (and difficult) to expand an ASP site with a class (base class).
Let's first choose a simple example:
We want to display the information of the Classic forum users, when you enter the user ID can, display some information about the user, this is a process, you can consider this, we regard the user as an object, he has attributes are ID, gender, integration, permissions, the implementation of the method has to display this information, OK, so write:
Class Blueidea
Private Bname,bpoint,bsex,blevel
'...................
End Class
Here we first declare a class named Blueidea, and then some private variables to store the properties of the Blueidea class, which are not accessible outside the code, which is data protection, to define these variables, to use the property statement to get the value indirectly to the private variable
'-----------------------------------------------------------------
Property Get GetName
Getname=bname
End Property
Property Let GetName (NameID)
Bname=nameid
If nameid= "" Then
Bname= "Not registered users"
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= "Male"
Else
bsex= "Female"
End If
End Property
'------------------------------------------------------------------
Property Get GetPoint
Getpoint=bpoint
End Property
Property Let GetPoint (Point)
Bpoint=killint (point,0,0)
End Property
'------------------------------------------------------------------
Here's a killint function, which is to judge the legality of data, and its prototype is:
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
The function is clear and no longer tedious to say.
Since we have to judge the user level by integral, a private function is defined here:
Private Function Getlevel ()
Bpoint=killint (bpoint,0,0)
If bpoint<500 Then
Blevel= "Junior Member"
ElseIf bpoint>=500 and bpoint<=100 Then
Blevel= "Senior Member"
Else
Blevel= "Ultimate Member"
End If
Getlevel=blevel
End Function
We need to be loopback user information and must define a public common function that displays information:
Public Function Showuser ()
Response.Write ("Response.Write ("Response.Write ("Getlevel
Response.Write ("End Function
End Class
Use this class when using this: (I wrote a form here to handle it)
Set blueideauser=new Blueidea
Blueideauser.getname=trim (Request ("id"))
Blueideauser.getsex=request ("Sex")
Blueideauser.getpoint=request ("point")
Blueideauser.showuser
If you want to see the effect, then look here: http://www.5do8.com/net/aspclass/class.asp
Controls the classes that read database information:
Reference source
' Name: Ado_5do8
' function: Read the operations of the database
' Source-cultivator village http://www.5do8.com Http://www.Blueidea.com-5do8
' Creation: 5do8
' Contact: 5do8@5do8.com
' Updated: November 13, 2005
' Authorization: Blue Ideal website points more than 3000, Cultivator village all registered users
' Class interface: Ado_5do8. connectstring= Database Absolute Path
' Ado_5do8.rs_top the number of calls, the name of the table
Class Ado_5do8
Private conn,sqlstr,rs,iid,itable,isession
' SQLSTR: Database address, absolute path, private
' Conn: Open the connection to the database, private
'------------------------------------------------------------------
REM eliminates some unwanted numbers
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= "Error occurred, cannot connect to database"
Response.Write (Mess)
Response. End
Else
mess= "Connection Database conn successful ...........<br/>"
Response.Write (Mess)
End If
End Sub
'----------------------------------------------------------------
Private Sub Closeconn ()
Conn.close
Set conn=nothing
Response.Write ("<strong style= ' color:red ' > Close conn Connection </strong>...End Sub
'-----------------------------------------------------------------
Private Sub Closers ()
Rs.close
Set rs=nothing
Response.Write ("<strong style= ' color: #085420 ' > Close Database 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 ("Response.Write ("<b>" &iRows+1& "</b> article Information Next
Response.Write "</ul><div style= ' top:20px;background-color: #ccc; color: #020; font-weight:bold;bordr-top : 2px solid #008;p adding:10px 0;color: #b00 ' > List (<a href=default.asp> back to typical mode </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% ' >
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
When in use:
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;p adding : 20px 5px;color: #008; font-weight:bold; " >
<label> Display Information total: <input name= "n" type= "text" maxlength= "4" size= "ten"/> per page number: <input name= "pagesize" Text "maxlength=" 4 "size=" value= "5"/><input name= "Arrstart" type= "hidden" value= "0" ></label>
</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 ("Response.Write "Show specific information below: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;p adding:20px 0;color:080;font-weight:bold;border-bottom:2px Solid #008 ' > page analysis completed, to update please select <a href=default.asp> back to the typical mode </a> parameters: Start, start element; PageNo, per page number </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
%>
Demo in: Http://www.5do8.com/net/aspclass