Discussion and comparison of ASP object-oriented programming

Source: Internet
Author: User
Tags comparison implement reference
Comparison | programming | objects | Comparing ASP is a dynamic Web-page programming technology introduced by Microsoft in the earlier period, but it combines the convenient and quick access of ADO to the database, combine XML, Com/activex and other technology to realize the function of the server multi-layer structure, it still has the tenacious vitality today, And still have a certain development. Asp. NET is completely different from ASP in architecture, but many of its built-in objects are also extended based on ASP. There are countless articles about ASP on the Web, but there is little introduction to ASP object-oriented and compared with other languages, which is why I am determined to write this article.
Because it was an early version, the ASP provided only a weak object-oriented interface. As we all know, the implementation language of ASP is divided into VBScript and Javascript/jscript: There is a class keyword in VBScript that can be used to declare a custom class; JavaScript is rather weird, it uses a function to "declare" classes, The function is then defined by This.prototype to define the property, This.func define the method. This will be discussed in VBScript, and the class declaration for VBScript is this:
class Name
statements
end Class
Here statements can declare public or private members, including functions, members, and properties. About attributes, we have to praise Microsoft's Get and set method, which appears in COM until the idea. NET has been used down, personally think for programmers, than Java with Getprop (), SetProp () two methods to achieve the same effect is more intuitive.
In contrast, the classes in VBScript are different from the classes in PHP4 (with the latest PHP5, of course), and the classes in VBScript keep the "Properties" of VB's incompletely object-oriented, which only implements the most basic constructs/destructors, member functions, variables, attributes, Even constructors cannot take parameters. In PHP4, the important properties of inheritance, function overload and so on are realized, and only if these are realized, can we call them object-oriented, which can provide the basis for realizing polymorphism. But neither has the function of implementing static members of the class. Although the same effect can be achieved with some other modifications, but from the object-oriented idea, this is not thorough (because PHP is very flexible, PHP4 can be static variables of the member function to implement the class static variables indirectly; and "::"--the operator that can implement static function access of class-- There is no strict inspection in the PHP4. In other words, all member functions can be accessed as static functions, so long as you do not use member variables in the function, there is no error. VBScript does not implement static at all and can only be implemented with session or application. So in normal use, you can use VBScript's custom class to encapsulate some operations, but don't expect it to be like C/Java/. NET that serves your object-oriented thinking.
vbscript also carry forward the default parameters or variables in VB is a good style of reference. In this way, although the script language is not sensitive to the type, it can also achieve the same effect as the pointer/reference in C + +, accomplishing many things. Most basic, such as using it to define a list of node class ListNode:
<%
Class ListNode
Public Content
Public NextNode

Private Sub Class_Initialize ()
Content= "Node"
Set nextnode=nothing
End Sub
End Class
%>
Oh, it is so simple, but do not feel contempt, also do not forget the initial value of the variable. VB is similar, the declaration of the type on the line. And when used:
<%
Set nh=new ListNode
Set NH. Nextnode=new ListNode
' Other statements ...
' Traversal list
Set N=NH
While not n are nothing
Response.Write n.content+ "<br/>"
Set N=n.nextnode
Wend
%>
If you do not add other code, the above run result is two "node". The custom classes and objects in VBScript are no more, as long as you have the basic concept and know something about it. Again, using the SET statement to assign a value to an object, equivalent to the assignment in Java, is to obtain a reference. This is more than PHP4. The default object assignment is to invoke a copy constructor to create a new object. Much better (even obj=new Obj; Such statements create two objects!). If you want to get a quote, add & to the variable before the equals sign, and it doesn't seem like PHP5 want to modify PHP4 's approach.
The session itself in asp can store objects, it can save basic variables, arrays, automation objects (Automation object), and so on, but can encounter problems when storing objects of a custom class. As in the following code:
<%
If IsEmpty (Session ("Node")) Then the Set session ("node") =new ListNode
Set n=session ("node")
Response.Write N.content
%>
Or the ListNode class, which is intended to retain only one ListNode object in a user session. So when a user accesses the page for the first time, a ListNode object is generated and saved in session ("Node"), and when the page is accessed, because session ("node") is not empty, a new object is not generated, but rather to the session (" Node ") to remove the saved object. Theoretically should also output 100, but the problem is that the ASP will always complain:
microsoft VBScript Runtime error ' 800a01b6 '
object doesn ' t support this property or method: ' N.content '
With N. Type can also be faulted. The same code is translated into PHP, and the runtime can be passed. Why?
Individual analysis down, think session can save object is true, but in VBScript the mechanism of type conversion is too weak, and there is no explicit mandatory type conversion for users to use, the session ("node") can not be correctly converted to the ListNode type. Because it is a custom class, we can only have the definition of the class in each page, so that in the case of ASP, each time the page is read, the ListNode class is a new class, so you do not recognize the object of this class in the session.
Conclusion: Try not to think of using session or application to store objects of custom classes in ASP. If you do, consider using COM to write the class, and then use the Set session ("obj") = Server.CreateObject ("Yourapp.yourclass") in VBScript to create an object, Then you can achieve the desired functionality.



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.