Class has appeared in ASP for a long time, but it is rarely seen being used in code. I remember when I was studying Dongwang’s forum program, I saw some examples, and I thought I admired it at the time - it was of no use to me. However, if Class is used less, it is at best just a packaging method for a large module. Only by using it for development on a large scale can its superiority in project management be shown. The so-called spaghetti style. code, it will come to an end with asp.
I think most asp programmers have not used Class yet, and are not familiar with the term object-oriented. I need to add a chapter to describe the role of classes in asp and its relationship with object-oriented programming. .
I will explain the use of Class in a way that is as close to programming practice as possible, but will not use abstract terms such as object-oriented. If you have a theoretical basis for object-oriented, you can combine these contents with it, or you will have your own unique Discovery. If you have never understood object-oriented, you don’t need to know what object-oriented is. As long as you learn the usage of Class by reading this series of articles, and follow the rules in programming, you can be robust enough. Manage your asp code.
Class is equivalent to a packaging box, which can package variables and functions to form a whole. All the content to be discussed in this series of articles can be said to be the knowledge of packaging.
For example, we can wrap a mathematical processing Class and use it:
Copy the code code as follows:
Class Con_Math
Public A
Public B
Private intC
Public Function Sum()
intC = cint(A) + cint(B)
Sum = intC
End Function
End Class
set math = new Con_Math
math.A = 19
math.B = 80
Response.write math.Sum
set math = nothing
Line-by-line explanation of syntax:
1. A packaging box is defined, named Con_Math. Everyone knows that Math means mathematics. The Con_ prefix is my programming habit. For Classes that may be used in the entire program, add a Con_ prefix to represent it. A universal Class. It is purely a programming habit, but adding a prefix is not only a matter of habit, which will be explained later.
2,3 put in two variables, named A and B. The Public in front of them means that they are public variables and can be accessed by external programs.
4 The variable intC is defined as Private, which means that it is a private variable inside the packaging box. It cannot be seen from the outside, nor can it be pulled out by force, no matter how hard you try. Steal it? Try it.
These four lines 6, 7, 8, and 9 define a function that performs addition operations. Except for one Public, the others are things you use every day. If you are not familiar with them... it is better not to read this series. It's better to do some basic homework first. Like the above two variables, adding a Public means that the packaging box has a hole here, and you can see it from the outside and use it.
10 End Class. Put on the sealing tape, and a neat Class will be packaged.
However, this packaging box is just an idea and cannot be used as a ready-made product. To borrow the words of Master Tang Seng: (Wukong wants to eat me) is just an idea and has not yet become a reality (why is he guilty of it)? It is better to wait for it to be realized. After it is transformed, it is not too late to use it again (it is not too late to condemn him).
Instantiation represents actual meat-eating behavior.
We can easily instantiate our idea in the asp code, but it is a pity that the thousands of monsters on Xitian Road cannot instantiate their Tang Monk-eating Class. It is really pathetic!
This is the advantage of programmers.
12 After instantiating the Class we defined, the instantiation name cannot be exactly the same as the conceptual name, otherwise the syntax will be messed up. So now you know, why is it a habit to add a prefix to Class? Of course, you can absolutely There is no need to use Con_ as a prefix, the following are all good materials: Yaomin_ Jimo_ Chunge_...or you can use your wife's maiden name. People should live a more imaginative life.
13,14 We assign values to the two public variables A and B. The number . here can be imagined as a hole in the packaging box. Through this hole, you can put things in and take them out. It must be Through this hole. Huh? Is it too small? No, no, don’t worry, you can put even bigger things in it. Just like a human mouth, even though it is small, it can eat up food as big as the earth.
15 We go through the small hole, take out the result of its function operation, and display it on the page, 99, auspicious, this is the number that adds up the first two digits and the last two digits of my birth year. Open a notepad and put Write this code and run it in IIS.
16 After eating at home, gay men have to wash the dishes and pots. The same is true for set ** = nothing. This is the same as clearing the Recordset after creating it. After setting nothing, it means that it is removed from the memory. If you throw it away, the space occupied by this universal packaging box will be freed up. Of course, the performance of your website will be improved, so why are you hesitating? Set nothing as soon as you are done using it.
I know that no one will play Function like this. At least Sum(19,80) is more convenient to use. Okay, let’s punch another hole in the packaging box:
Copy the code code as follows:
Class Con_Math
Public A
Public B
Private intC
Public Function Sum()
intC = cint(A) + cint(B)
Sum = intC
End Function
Public Function Sum2(ByVal vA,ByVal vB)
Sum2 = cint(vA) + cint(vB)
End Function
End Class
set math = new Con_Math
Response.write math.Sum2(19,80)
set math = nothing
Do you still want Ctrl+C?
Although you look at me so sincerely, you still have to say whether you want to learn. It’s impossible that you don’t want to learn but I want you to learn, and it’s also impossible that you want to learn but I won’t let you learn. Do you really want to learn? confirm?
Then don't use Ctrl+C.