Microsoft ASP. NET Ajax framework entry (7) namespace, class, inheritance, Interface

Source: Internet
Author: User
Vs 2008

This article describes how to use Microsoft ASP. NET Ajax library to write namespaces, classes, inheritance, interfaces, and other ooCode

1. Prepare
Create an "ajax client library" script file: person. js
Add the scirptmanager control to the default. aspx page:

< ASP: scriptmanager ID = " Scriptmanager1 " Runat = " Server " >
< Scripts >
< ASP: scriptreference path = " ~ /Person. js "   />
</ Scripts >
</ ASP: scriptmanager >

2. namespace
Add the code in person. js to register the namespace we defined:

Type. registernamespace ( " Tristan " );

3. Class
Now we define a person class in person. JS:

Tristan. Person = Function (name) {
This. _ Name=Name;
}
Tristan. Person. Prototype =   {
Get_name: function () {
Return This. _ Name;
} ,
Set_name: function (name) {
This. _ Name=Name;
} ,
Sayhello: function () {
Alert ('I am a person, my name is' + This. _ Name );
}
}

Tristan. Person. registerclass ( " Tristan. Person " );


Compile the test code:VaR P= NewTristan. Person ('Guozhijian');
P. sayhello ();

Run: the prompt box "I am a person, my name is guozhijian" appears"
 
4. Inheritance
Now write a student class inherited from the person class:

Tristan. Student = Function (name) {
Tristan. Student. initializebase (This, [Name]);
}

Tristan. Student. registerclass ( " Tristan. Student " , Tristan. person );

Two steps to implement inheritance:
1) The initializebase method is called in the subclass constructor. The first Param is the keyword this, and the second parameter is the array composed of the constructor parameters.
2) Call the registerclass method at the end of the class code to indicate the parent class.

Compile the test code:VaR s= NewTristan. Student ("Zhenglanzhen");
S. sayhello ();

Run: the prompt box "I am a person, my name is zhenglanzhen" appears"

Now I want the override sayhello method: Tristan. Student. Prototype =   {< br> sayhello: function () {< br> alert ( ' I am a student, my name is ' + This .
}
}

Run: the prompt box "I am a student, my name is zhenglanzhen" appears"

To change it, I need to call the sayhello method of the parent class first, and then the sub-class's own logic, so:

Tristan. Student. Prototype =   {
Sayhello: function () {
Tristan. Student. callbasemethod (This,'Sayhello');
Alert ('Actually I am a student');
}
}


Implementation steps: Call the callbasemethod method. The first parameter is the this keyword, the second parameter is the method name of the parent class to be called, and the third parameter is optional, if this method of the parent class has parameters, it is an array consisting of the parameter list.
Run: the prompt box "I am a person, my name is zhenglanzhen" appears"
The prompt box "actually I am a student" appears"

5. Interface
Now let student have a walking behavior, define an interface icyclable: Tristan. iretriable = Function () {
ThrowError. notimplemented ();
}
Tristan. iconfigurable. Prototype =   {
Walk: function (){
ThrowError. notimplemented ();
}
}
Tristan. icyclable. registerinterface ( " Tristan. iretriable " );

Steps:
1. An exception is thrown in all methods to avoid interface instantiation.
2. Call the registerinterface Method

Now let the student class implement the iretriable interface and make the following changes to the Student Class:Tristan. Student. registerclass ("Tristan. Student", Tristan. Person, Tristan. iretriable );

Indicates that the student class implements the Tristan. iretriable interface. If you want to implement multiple interfaces, add parameters later.

Compile the test code:VaR s= NewTristan. Student ("Zhenglanzhen");
S. Walk ();

Run: an error is reported! Ah, the walk method has not been implemented yet.

Make the following changes to student: Tristan. Student. Prototype =   {
Sayhello: function () {
Tristan. Student. callbasemethod (This,'Sayhello');
Alert ('Actually I am a student');
} ,
Walk: function () {
Alert ('I am walking');
}
}

Run the test code again. The prompt box "I am walking" appears"

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.