.AFR_Article_title
{
Font: Normal Bold 14px "Tahoma";
}
.AFR_CONTENTS
{
Font: Normal Normal 12px "Tahoma";
Line-height: 22px;
}
.AFR_CODE
{
FONT: NORMAL NORMAL 11PX "Courier New";
Line-height: 20px;
Width: 100%;
Background-color: #eeeeee;
Padding: 5 10 5 10;
}
.AFR_COMMON
{
Color: # 55bb55;
}
.AFR_STRING
{
Color: # eE0000;
}
Recently, because of the school design, I have been working on the ASP and the database. I am doing a Java program design in the previous stage. Suddenly received the task and learned ASP, so I have always been hot and good at using JavaScript to architecture ASP procedures. A obvious advantage of JavaScript is that it can define and hold their own objects. This seems to be that VBScript cannot match. With this, JavaScript can be used to make closer to object-oriented programming. Maybe this will make the website more fun ... but there is a serious shortcoming! JavaScript does not support inheritance mechanisms. Not like Java, support Extends keyword (although this keyword is reserved in JavaScript). In Microsoft's ASP.NET, JavaScript began to provide a relatively perfect support for inheritance mechanisms. There is also a support of the inheritance mechanism. These are all empty ... but now I can't convince the old man's old man to buy a better domain space, but I don't want to endure the suffering of the inheritance mechanism in the ASP, so urgent life, There is also some results! JavaScript does not support inheritance mechanisms at all! This is certain. But we can find a way to do some hands and feet, simulate one out. Waiting a bunch of nonsense, first look at an example: function persons ()
{
Public: // Note this public!
THIS.GETNAME = PERSON_MFGETNAME;
Private: // Note this private!
THIS.M_STRNAME = "Guest";
}
Function persons_mfgetname ()
{
Return this.m_strname;
}
Var myperson = new person ();
Myperson.getname ();
Note Two keywords in the program above: private and public. In fact, there is no such use, this is just my habits. Fortunately, it will not be wrong in practical applications. You can use any output statements to view the results. Of course, the results will appear in the browser window: Guest words. This is the first step! Here is a key step: inheritance! Function Student () // Extends Class: Person
{
EXTENDS: / / Note this extends cannot use lowercase letters
THIS.SUPER = Person; // Define points to its "parent constructor". SUPER can not use underground form
THIS.SUPER (); // Call its "parent constructor"
Private:
THIS.M_NSTUDENTID = 0;
}
This is also my habit using Extends, but I must not use lowercase letters. Because Extends is the reserved word in JavaScript. Subsequently define and call the "Parent Class Constructor" to "inherit" all properties and methods there from the "parent class". Although there is no GetName () method in Student, it can be called. Because he has inherited Person's getName () method. Var mystudent = new student (); myStudent.getName (); // call the "parent class" getName (), the result is to return "GUEST"
This is true about JavaScript inheritance. As long as you keep in mind two steps:
In "Subclass", first define a function pointing to "Parent Class Constructor" (what name can, I am used to using super) This function can then inherit all the properties and methods of "parent classes"! The principle of this "inheritance mechanism" is very simple. Its principle is a bit similar to the usage of include keywords in the C / C language. When a C / C source program is compiled, the compiler replaces the include field directly into the entire contents of its files. The same is true for JavaScript inheritance. First, a "class" member function points to "Parent Class Constructor", in the subsequent function call, naturally add all members "to this" class ". I am skeptical now, since Extends and Super are reserved, what JavaScript does not support inheritance? I don't know if there is any other better way? I hope everyone will guide ...
Continue to discuss the JavaScript inheritance mechanism. However, let's take an example before continuing to discuss: function shape ()
{
PUBLIC:
this.calcuarea = shape_mfcalcuarea; // calculate the area
}
Function shape_mfcalcuarea ()
{
// ...
}
Obviously, the Calcuarea () method under this Shape class should be an abstract function. In Java, you can use the Abstract keyword to define abstract functions, and this feature cannot be supported in JavaScript. The abstract function should not have a function, or in other words: When calling shape.calcuarea (), it should be an error and terminate the ASP script. Some design experiences from PHP programming, we can do this: function shape_mfcalcuarea ()
{
Response.write ("Error, Method Shape_Mfcalcuarea Is Abstract");
Response.end ();
}
In doing so, "forcing" to write code overload Calcuarea () methods in the inheritance class of Shape. You can also directly let the Calcuarea function under the Shape class is equal to NULL, allowing the server to point out the error in the run. In the inheritance class of Shape, we can simulate overloading of the Calcuarea method by changing the function points to the CalCuarea variable. Function Rect ()
{
EXTENDS:
THIS.SUPER = Shape;
This.super ();
OVERRIDE:
THIS.CALCUAREA = Rect_mfcalcuarea;
}
Function Rect_mfcalcuarea ()
{
// ...
}
If a class contains an abstract function, then this class must be an abstract class! Abstract classes cannot be instantiated, must be inherited by subclasses. Unfortunately, JavaScript does not support abstract class definitions (if JavaScript really supports the "abstract class" mechanism, then "inherit" mechanisms must be supported. How to implement an abstract class mechanism, I think it is similar to the abstraction method above. Or use the response.end () to force the program, and the programmer before this: "This class is an abstract class." But different is this method to be used in the "constructor" in the class. This allows an abstract class that cannot be instantiated. JavaScript is special, tie the class definition and constructor together. So is this form? Function shape () {
Response.write ("Error, Class Shape Is Abstract");
Response.end ();
// Other code ...
}
Var myshape = new shape (); // will be wrong to defend an error OK
Function Rect ()
{
EXTENDS:
THIS.SUPER = Shape;
This.super ();
}
Var myRect = new reference (); // will still be defeated an error OH, NO
Because Shape is an abstract class, a warning appears when declaring the first myshape object. This is what we expect. The Rect class has inherited the Shape class, but it is still the "error, class shape is abstract" of the defendant. This is my regret for my JavaScript inheritance. It is necessary to "change" to implement the abstraction class "Perfect". I still use a smart method that is more stupid. Say it "stupid" because this method is achieved by judging the variable value. And said it is "savvy" because this method uses the least variable. Function shape ()
{
IF (! this.super)
{
Response.write ("Error, Class Shape Is Abstract");
Response.end ();
}
// Other code ...
}
Var myshape = new shape (); // will be wrong to defend an error OK
Function Rect ()
{
EXTENDS:
THIS.SUPER = Shape;
This.super ();
}
Var myRect = new Rect (); // Successfully inherit Yes, OK