How does ASP talk about applying to classes? _asp class Classes

Source: Internet
Author: User
Tags inheritance repetition
First excerpt Tenkine Yang teacher An article in a paragraph:
Object-Oriented Programming

With the complexity of program design increasing, the method of structured programming is not enough. The root cause of lack of use is "code reuse" when inconvenient. The object-oriented approach is born, and it implements a more complete code reuse function through inheritance. Many students in the job, interview, often asked a question "You come to talk about what is object-oriented programming," Students speechless, come back to ask me, this question should be how to answer. I told him, you just have to say a word. "object-oriented programming is the encapsulation of data; the programming of Paradigm (template) is the encapsulation of algorithm. "Later, students encountered this problem, only a simple one-talk, the other side on the students on the surprise (the students later proudly told me). Why is that? Because only through thorough experience and practice can we extract this essence.

Object-oriented design methods and ideas, in fact, as early as the early 70 has been proposed. The aim is to force the program to manipulate the data in a functional way. This enables the encapsulation of the data to avoid the previous design method, any code can easily manipulate the data due to the bug, and find to modify the bug is very difficult. So you can say that even if I don't use object-oriented, when I want to access a certain data, I just call the function to access it? Yes, it does, but it's not mandatory. People have inertia, when I want to add 1, why do I have to call the function ah? Forget it, direct i++ more convenient. Oh, officially because of this lazy, when the program out of the bug, it can not be caught. And object-oriented is mandatory, from the compile phase to solve your lazy problem.

Coincidentally, object-oriented thinking, in fact, and our daily life to deal with the problem is consistent. For example, I'm going to throw away a teacup. Too easy, pick up the teacup, go to the trash, throw it! To analyze this process, we first select an "object"------a teacup, and then apply an action to the object-throw. There is a certain limit to what each object can exert on it: the teacup, which can be tossed, can be smashed, can be used to drink water, can be knocked out by the sound ... A piece of paper, can be written, can be torn, can be burned .... In other words, once an object is identified, the method is then determined. This is the way we live our lives. But let's think about our programming and the operation of computers, but that's not the case. For the DOS operation, I'm going to delete a file by:c:> del filename < carriage return > at the DOS prompt. Notice this process, the action in the Front (DEL), the object in the back (filename), and the Object-oriented method in exactly the opposite order. So it's just a matter of order, what's the impact? Oh, you must have seen this phenomenon: File not found. "Ah ~ ~ ~, I was wrong, I was wrong, the filename knocked out a letter", then re-enter the:c:> del filename 2< carriage return >. Unfortunately it happened again, the computer report: File Read only. Haha, pain:). So the DOS operation is actually a violation of our daily habits (of course, no one has raised objections before), and now because of the use of object-oriented design, then these problems, in the compile time resolved, rather than at the time of the run. Obj.fun (), for this statement, whether it is an object, or a function, if you enter a problem, it will be compiled at the time of the report out, to facilitate you to modify, rather than in the execution of the time error, harm you go around to catch worms.

At the same time, object-oriented can solve the problem of code reuse-inheritance. I used to write a "dog" class with attributes (variables): Hairy, 4-legged, with a cocked tail (a wolf with a tail hanging), a sharp nose, and a taste for meat bones ... The method has (function): can run, can smell, bark bark ... If it goes to catch mice, they call it "meddling." Well, the dog is well written. But in my actual life, my domesticated dog is very similar to this "dog" that I wrote before, only a little different, that is my dog, which is: Curly and long, small nose, small mouth ... So, I derive a new type, called "Pug class" on the basis of "dog", plus new features. Well, the program is finished, and it's reusing the right old code--that's the benefit of object-oriented programming. My success was just standing on the shoulders of giants. Of course, if you use VC, the most reusable code is MFC's class library.

============================================================

OK, so let's see how we use ASP.

It is common to use the default scripting language of IIS as the server-side execution language of ASP, in the beginning of the basic is to mix the VBS script and HTML together to achieve some functionality. For example, if you need to display the latest 5 records on the current page, you can do so.

1, first define the connection of the database, such as:


Db_path = ".. /database/cnbruce2005.mdb "
Set conn= Server.CreateObject ("ADODB.") Connection ")
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" &server.mappath (Db_path)
Conn. Open ConnStr



2, then set up a collection of database records, extract the relevant information


Set rs = Server.CreateObject ("ADODB.") Recordset ")
sql = "SELECT Top 5 * FROM [News] ORDER by n_id Desc"
Rs. Open sql,conn,1,1



3, the last way to show the data through the loop


Do as not rs.eof
Response.Write rs ("N_title")
Rs.movenext
Loop
' and the final shutdown and release operation.
Rs.close
Set rs=nothing



So it is in Response.Write rs ("N_title") here, for the final web design needs, in its backwards and forwards, it is estimated that the need to add some other HTML tag elements. So it's natural that the VBS script and HTML are blended together.

Again, if there are a lot of pages need to display these 5 records, that is not every page to set this way, of course, the most important thing is that the design of each page is different. So that's the repetition. Repeat over there? Can you not repeat there?

1, for the connection to the database, have directly established a database connection file conn.asp, content as above.

2, as long as the need to use the database, you need to establish a connection, directly containing the reference to the database connection file.

<!--#include file= "conn.asp"-->

So what's the redundancy on the top? Repeat for each database connection. This is an anti duplication for some common code that is commonly used. Next look, if I want this page to display 5, that page shows 6, what about it? Obviously, this can only be a simple modification to the SQL statement on the current page, such as Top 5, which is the top 6.

OK, keep looking, is there any repetition? Yes, except for the different definitions of SQL queries, the rest are duplicates.

So, continue to find a way: Can you define the number of extraction? Is that I want to extract a few on a few, but the program only needs to write one. Then the function comes in handy. For example, I define a function like this:



Function topnews (Tnum)

Set rs = Server.CreateObject ("ADODB.") Recordset ")
sql = "SELECT top" &tnum& "* from [News] ORDER by n_id Desc"
Rs. Open sql,conn,1,1

Do as not rs.eof
Response.Write rs ("N_title")
Rs.movenext
Loop

Rs.close
Set rs=nothing

End Function



Then, you can use Topnews (5) or topnews (6) to complete the required

...... At first glance, it seems that the ASP uses the function of this even if the final completion. Because the main program function is made function module, need to use this function in front page, call directly, if necessary modify the function parameter value is a perfect ending/.

So, how to introduce the class in ASP? What is this kind of thing? And how is it applied?

In the ASP scripting language, VBScript has a class keyword that you can use to declare a custom class. Like what

Class Name
statements
End Class

Here statements can declare public or private members, including functions, members, and properties.

JavaScript, in turn, uses a function to "declare" the class, and then define the property by This.prototype in that function, This.func define the method. Here is an article to see.

Which one is easy to pick. What can be done with the name of the class that defines it?

The first one. MSDN Documentation: In Visual Basic 6.0, when you create and destroy class modules, class modules use Initialize and Terminate events to perform all the necessary actions. When an object is first encountered after the NEW statement, the Initialize event is raised and the Terminate event is raised immediately when the last reference to the object is disposed. However, these event methods can be called directly at any time during execution.

So it's common to see examples like this:


Class Cnbruce ' declares a classes named Cnbruce

Private Cnrose
Private Sub Class_Initialize
Cnrose= "My Name is Cnrose."
End Sub

Private Sub Class_Terminate ()
End Sub

End Class



Here, this is also an initialization.

In addition, the ASP class has a let and get method. For example, in the above code class Cnbruce ... End class to join:

Public Property Get Yname
Yname=cnrose
End Property

It becomes:


Class Cnbruce ' declares a classes named Cnbruce

Private Cnrose
Private Sub Class_Initialize
Cnrose= "My Name is Cnrose."
End Sub

Private Sub Class_Terminate ()
End Sub

Public Property Get Yname
Yname=cnrose
End Property

End Class



So how do you extract this value, like

Set aaa=new Cnbruce
Response.Write AAA. Yname

Pay attention to AAA. Yname has felt something. AAA is the object defined as the Cnbruce class, and the final output is shown as the Yname get value in the class object, whose value is the value of the variable cnrose, and the value is already initialized, so the final result is the "my Name is Cnrose."

This is get, which directly extracts the inside of the class that has been encapsulated, so let? That is, an external interactive access operation to the class. For example, I now externally defined values applied to the class. The first definition is:

Public Property Let Mname (NNN)
cnrose=nnn
End Property

It means very simply that the value of the parameter nnn will be assigned to the variable cnrose with different values from the outside. Then OK combination of the program, that is, the most output display content, that is, the display of variable cnrose content, not initialized content, but by let external application of the indefinite value. So how to apply let it.

Aaa. Mname= "SDASDASD"

It's OK to define the value directly. So now look at all the programs:


<%
Class Cnbruce

Private Cnrose
Private Sub Class_Initialize
Cnrose= "My Name is Cnrose."
End Sub

Public Property Get Yname
Yname=cnrose
End Property

Public Property Let Mname (NNN)
cnrose=nnn
End Property

End Class

Set aaa=new Cnbruce
Aaa. Mname= "Hahahoho"
Response.Write AAA. Yname

%>


Although the definition is AAA. The value of the Mname is "Hahahoho", but according to the public Property Let Mname (NNN), the value to the variable is cnrose,so at the time of the output aaa.yname, It is no surprise that the value of the result is nothing. /

As for functional programs, it is defined in the class as function functions, or sub program sub.

So, in general, it's just the encapsulation of functionality and good writing when applied (direct class name. method, class name. Attribute-_-!), but don't expect it to have object-oriented thought services like Java or. Net.

Time is limited.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.