As the complexity of the program design increases, the structured programming method is not enough. The root cause of not enough is that it is inconvenient to reuse the code. The object -oriented method was born, and it realized a comprehensive code reuse function by inheriting. Many students are applying for work. During the interview, they are often asked about a question about what is the object -oriented program design. The students are speechless and ask me how to answer this question. I told him that as long as you say a word, it is enough for object -oriented programming to pack the data; the program design of the paradigm (template) is an encapsulation of the algorithm. Later, a student encountered this question again, and only a simple answer, the other party looked at the student (the student later told me pride). Why? Because only after a thorough experience and practice, this essence can be refined.
Object -oriented design methods and ideas have actually been proposed as early as the early 1970s. The purpose is: the compulsory program must manipulate the data by function. In this way, the packaging of data avoids the previous design method. Any code can be operated by any code. It is very difficult to find modification to modify this bug. Then you can say that even if I don't use the object -oriented object, when I want to access a certain data, can I access it by calling the function? Yes, it is true, but it is not forced. People are inert. When I want to add 1 to I, why do I have to call a function? Forget it, directly I ++. Haha, because of this laziness, when the program comes out of the bug, it is not easy to catch. Object -oriented is compulsory, and you have solved your lazy problem from the compilation stage.
Coincidentally, the object -oriented thoughts are actually in line with our daily life. For example, I plan to throw off a tea cup, how can I throw that? Too simple, pick up the tea cup, walk to the trash can, throw! Pay attention to analyzing this process. We first choose an object-the tea cup, and then apply a move to this object-throw. The action that each object can be applied on it is limited: the tea cup can be thrown, it can be smashed, can be used to drink water, you can knock it out ...; a piece of paper, you can, you can, you can, you can, you can, you can, you can, you can, you can, you can, you can, you can, you can, you can, you can, you can, you can, you can, you can, you can, you can, you can, can Written, you can tear, you can burn ... In other words, once a object is determined, the method will be determined. This is how our daily life is. However, everyone thinks about our programming and the operation of computer, but this is not the case. Take the DOS operation, I want to delete a file, the method is under the DOS prompt: C:> Del file name <Enter>. Pay attention to this process, the movement is in front (DEL), the object is in the back (file name), which is the opposite of the object -oriented method. So is it just a question, what impact will it bring? Haha, everyone must have seen this phenomenon: file not found. Essence Unfortunately, the computer reports: File Read only. Haha, pain :). Therefore, the operation of DOS actually violates the habit of our daily life (of course, no one has raised objections before), and now because of the use -oriented design, these problems are solved when compiling, not, not the time, not, not the time, not, not, not, not, not, not, instead of the compilation, not, not, not, instead of the compilation, not, not, instead of the compilation, it is not solved, not, not, not, not, instead of the compilation, not, not, instead of the compilation, it is not solved, not, instead of the compilation, not, instead of being, When running. obj.fun (), for this statement, whether it is an object or a function, if you enter a problem, then it will be reported when compiled, which is convenient for you to modify, instead of making mistakes during execution. Catch the worm.
At the same time, the object-oriented problem can solve the problem of code reuse-inheritance. I used to write a dog category with attributes (variables): hairy, 4 legs, tails with tilted tails (the one who pulled the tail is a wolf), my nose is very sensitive, I like to eat meat and bones ... . The method is (function): can run, smell, and bark ... if it grasp the mouse, people call it a lot of nos. Well, the dog is written. But in my actual life, the dog I raised is very similar to the dog I wrote before, only a little different, that is, my dog, it is: curls and long, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose, small nose. , Small mouth ... As a result, I sent a new type, called Haba dogs on the basis of dogs, plus new features. Well, the program is finished, and it is reusable for the previous correct code-this is the benefits of object-oriented programming. My success was just standing on the shoulders of the giant. Of course, if you use VC, the most reuse code is the Library of MFC.
OK, then see how we use ASP.
Everyone generally uses IIS's default script language VBSCRIPT as the ASP server to perform language. At first, the VBS script and HTML were mixed together to achieve certain functions. For example, you need to display the latest five records on the current page, and that's it.
1. First define the connection of the database, such as::
The following is the reference content:
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 establish a database recording collection, extract related information
The following is the reference content:
Set rs = server.createObject (Adodb.oldSet)
SQL = SELECT TOP 5 * From [News] Order by N_ID DESC
RS.Oopen SQL, CONN, 1,1
3. Finally, the data is displayed through the loop method
The following is the reference content:
do while not rs.eof
response.write RS (n_title)
rs.movenext
loop
'There is also the final shutdown and release operation
RS.Close
set rs = Nothing
Then it is here in Response.write RS (N_Title). For the need for the final web design, it is estimated that some other HTML tag elements need to be added before and after. So naturally, the VBS script and HTML were mixed.
Let's look at it, if there are many pages that need to display these 5 records, it is necessary to set this way every page. Of course, the most important thing is that the design of each page is different. That's how to repeat. Where is it repeated? Where can I not repeat it?
1. For the connection of the database, a database connection file Conn.asp is directly established, and the content is as above.
2. As long as the database needs to be used, the connection needs to be established, and it can directly include the reference to the database connection file.
<!-#Include file = conn.asp->
So what is the duplication above? Each database connection is repeated. This is the anti -repeat of some common code. Then, if you say that I want this page to display 5 and 6 shows 6, what should I do? Obviously, this can only be simply modified to the SQL statement in the current page. For example, the original TOP 5 was modified to TOP 6.
OK, continue to watch, is there still repeated? Yes, except for SQL query definition, others are repeated.
So, continue to find a way: Can you define the extracted number of extraction? I just want to extract a few, but the program only needs to write one. Then at this time, the function is in handy. For example, I define this function:
The following is the reference content:
Function Topnews (TNUM)
Set rs = server.createObject (Adodb.oldSet)
SQL = SELECT TOP & Tnum & * from [News] Order by N_ID DESC
RS.Oopen SQL, CONN, 1,1
do while 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 need
...... At first glance, it seems that the ASP uses the function, which is finally completed. Because the main program function is made into a function module, you need to call it directly when using this function on the front desk page. If necessary, modify the function parameter value of the function is a perfect ending/.