An analysis of object-oriented ideas of ASP.net Ajax

Source: Internet
Author: User
Tags implement inheritance integer reflection tostring
Ajax|asp.net| Object The long-awaited asp.net AJAX v1.0 official edition was finally released. Now you can easily write rich, interactive Web applications with Microsoft asp.net ajax JavaScript. Of particular concern is the increase in object-oriented support for Microsoft AJAX Library, which previously did not support object-oriented development. Icrosoft AJAX Library now supports classes, namespaces, inheritance, interfaces, enumerations, reflections, and so on. These new additions are similar to the. NET Framework, which makes developing asp.net Ajax applications easier to maintain and easier to expand. Now let's look at how the Microsoft AJAX Library supports these features.
1. Class, Member, and namespace
In the Microsoft AJAX Library, all JavaScript classes inherit from object (similar to the. NET Framework library, all inherited from Object), in the ASP.net In AJAX applications, you can use object-oriented programming patterns to create objects and components that inherit from the Microsoft Ajax base class, which have four types of Members: Fields, properties, methods, events. Fields and properties are name/value pairs that describe the attributes of an instance of a class. Fields are composed of simple types and can be accessed directly, for example:
Myclassinstance.name= "Fred".
A property can be any simple type or reference type, accessed through the get and set methods. In ASP.net Ajax, get and set are independent functions and specify that the prefix "get_" or "set_" be used in the function name, such as when you want to obtain or set the value of the Cancel property, you can invoke the Get_cancel or Set_cancel method.
One method is to complete an active function instead of returning the value of an attribute. Properties and methods are demonstrated in the following examples.
An event indicates that a specified action occurs. When an event occurs, it can call one or more functions. The event owner can complete any task that waits for an event to occur.
Namespaces are logical groupings of the associated classes. Namespaces allow you to group public functions.
In order for ASP.net web pages to have asp.net ajax features, you must add <asp:ScriptManager> controls to the page, and when the page starts, the scripts referenced asp.net Ajax libraries are automatically generated.
The following example shows that the page uses the <asp:ScriptManager> control.
<asp:scriptmanager runat= "Server" id= "ScriptManager"/>
The following example shows how to use the Type.registerNamespace and. RegisterClass methods to add the person class to the demo namespace, create the class, and then register the class.
Type.registerNamespace ("Demo");

Demo.person = function (FirstName, lastName, EmailAddress) {
This._firstname = FirstName;
This._lastname = LastName;
this._emailaddress = EmailAddress;
}

Demo.Person.prototype = {

Getfirstname:function () {
return this._firstname;
},

Getlastname:function () {
return this._lastname;
},

Getname:function () {
return this._firstname + ' + this._lastname;
},

Dispose:function () {
Alert (' Bye ' + this.getname ());
}
}
Demo.Person.registerClass (' Demo.person ', null, sys.idisposable);
The class person is defined in the script file Namespace.js, and the namespace of the class is "Demo". Run the page namespace.aspx and click the button to create an instance of the Demo.person class.

2. Access Modification
Many object-oriented programming languages have the concept of access modification. Allows you to specify that a class or member is valid within a certain range. For example, a program that can be executed externally, an internal class with the same namespace, or a class within a specified code fast. There is no access adornment in JavaScript, but in asp.net ajax the following underscore character "_" is considered private and cannot be accessed outside of the class.
3. Inheritance
Inheritance is the ability of a class to derive from another class. Derived classes automatically inherit all the fields, properties, methods, and events of the base class. A derived class can change the behavior of a member by adding a new member or overriding a member that already exists in the base class.
The following script instance has two classes of person and employee,employee inherited from person, and two classes demonstrate the use of private fields, all of which have public properties, methods. In addition, the employee class overrides the ToString implementation of the person class and invokes the functionality of the base class.
Type.registerNamespace ("Demo");

Demo.person = function (FirstName, lastName, EmailAddress) {
This._firstname = FirstName;
This._lastname = LastName;
this._emailaddress = EmailAddress;
}

Demo.Person.prototype = {
Getfirstname:function () {
return this._firstname;
},

Getlastname:function () {
return this._lastname;
},

Getemailaddress:function () {
return this._emailaddress;
},
Setemailaddress:function (EmailAddress) {
this._emailaddress = EmailAddress;
},

Getname:function () {
return this._firstname + ' + this._lastname;
},

Dispose:function () {
Alert (' Bye ' + this.getname ());
},

Sendmail:function () {
var emailaddress = this.getemailaddress ();

if (Emailaddress.indexof (' @ ') < 0) {
EmailAddress = EmailAddress + ' @example. com ';
}
Alert (' sending mail to ' + EmailAddress + ' ... ');
},

Tostring:function () {
return This.getname () + ' (' + this.getemailaddress () + ') ';
}
}
Demo.Person.registerClass (' Demo.person ', null, sys.idisposable);
Demo.employee = function (FirstName, LastName, EmailAddress, team, title) {
Demo.Employee.initializeBase (This, [FirstName, LastName, EmailAddress]);

This._team = Team;
This._title = title;
}

Demo.Employee.prototype = {

Getteam:function () {
return this._team;
},
Setteam:function (Team) {
This._team = Team;
},

Gettitle:function () {
return this._title;
},
Settitle:function (title) {
This._title = title;
},
Tostring:function () {
Return Demo.Employee.callBaseMethod (this, ' toString ') + ' \ r \ n ' + this.gettitle () + ' \ r \ n ' + this.getteam ();
}
}
Demo.Employee.registerClass (' Demo.employee ', Demo.person);

There are two classes defined in the Inheritance.js script file: Person and Employee,employee are inherited from. Each class has fields, public properties, and methods. In addition, the employee class overrides the implementation of ToString and invokes the functionality of the base class in the overridden code. In this example, the name space of the class person is set to "Demo". To run the page inheritance.aspx, click "Create Objects," "Object release," "Public and private properties," "Object Methods," "Rewrite Methods," "Object type checking" to experience.
4. Interface
An interface is a logical protocol that a class implements and is a public compliance specification for the integration of classes. It enables multiple classes and the same interface to combine implementation definitions with the concrete implementation of a class. The following example defines a base class tree and interface Ifruittree,apple and banana two classes that implement the interface Ifruittree, but the Pine class does not implement interface Ifruittree.
Type.registerNamespace ("Demo.trees");

Demo.Trees.IFruitTree = function () {}
Demo.Trees.IFruitTree.Prototype = {
Bearfruit:function () {}
}
Demo.Trees.IFruitTree.registerInterface (' Demo.Trees.IFruitTree ');


Demo.Trees.Tree = function (name) {
This._name = name;
}
Demo.Trees.Tree.prototype = {
Returnname:function () {
return this._name;
},

Tostringcustom:function () {
return This.returnname ();
},

Makeleaves:function () {}
}
Demo.Trees.Tree.registerClass (' Demo.Trees.Tree ');


Demo.Trees.FruitTree = function (name, description) {
Demo.Trees.FruitTree.initializeBase (this, [name]);
this._description = description;
}
Demo.Trees.FruitTree.prototype.bearFruit = function () {
return this._description;
}
Demo.Trees.FruitTree.registerClass (' Demo.Trees.FruitTree ', Demo.Trees.Tree, Demo.Trees.IFruitTree);

Demo.Trees.Apple = function () {
Demo.Trees.Apple.initializeBase (this, [' Apple ', ' red and crunchy ']);
}
Demo.Trees.Apple.prototype = {
Makeleaves:function () {
Alert (' medium-sized and desiduous ');
},
Tostringcustom:function () {
Return to ' Fruittree ' + Demo.Trees.Apple.callBaseMethod (this, ' tostringcustom ');
}
}
Demo.Trees.Apple.registerClass (' Demo.Trees.Apple ', Demo.Trees.FruitTree);

Demo.Trees.GrannySmith = function () {
Demo.Trees.GrannySmith.initializeBase (this);
You are must set the _description feild after initializebase
Or you'll get the base value.
This._description = ' green and sour ';
}
Demo.Trees.GrannySmith.prototype.toStringCustom = function () {
Return Demo.Trees.GrannySmith.callBaseMethod (this, ' tostringcustom ') + ' ... its grannysmith! ';
}
Demo.Trees.GrannySmith.registerClass (' Demo.Trees.GrannySmith ', Demo.Trees.Apple);


Demo.Trees.Banana = function (description) {
Demo.Trees.Banana.initializeBase (this, [' Banana ', ' yellow and squishy ']);
}
Demo.Trees.Banana.prototype.makeLeaves = function () {
Alert (' Big and green ');
}
Demo.Trees.Banana.registerClass (' Demo.Trees.Banana ', Demo.Trees.FruitTree);



Demo.Trees.Pine = function () {
Demo.Trees.Pine.initializeBase (this, [' Pine ']);
}
Demo.Trees.Pine.prototype.makeLeaves = function () {
Alert (' Needles in clusters ');
}
Demo.Trees.Pine.registerClass (' Demo.Trees.Pine ', Demo.Trees.Tree);
A tree base class and a Ifruittree interface are defined in the Interface.js script file. Apple and banana Two inheriting classes implement the Ifruittree interface, and the Pine class does not implement the Ifruittree interface. Run interface.aspx, click on "Object Creation", "Interface Check", "Call interface method" to experience.
5. Enumeration
An enumeration is a class that contains a set of named positive integer constants. You can access its value like an Access property. For example:
Myobject.color = mycolorenum.red, enumerations provide an easy to understand integer representation. The following example defines an enumeration type whose color is named as a meaningful name in hexadecimal notation.
type.registernamespace ("Demo");

Define An enumeration type and register it.
Demo.color = function () {};
Demo.Color.prototype =
{
red:0xff0000,
BLUE:0X0000FF,
GREEN:0X00FF00,
White:0xffffff
}
Demo.Color.registerEnum ("Demo.color");
Run enumeration.aspx, select the color in the Drop-down box, and the script sets the background color to the color in the selected enumeration type Demo.color.
6. Reflection
Reflection is used to check the structure and composition of a running program, is reflected through the API of class type, which enables you to collect information about an object, such as which class it inherits, whether it implements a specified interface for a class, whether it is an instance of a class, and so on.
The following example tests whether the Grannysmith class implements the preceding interface with a reflection API. Run reflection.aspx, click "Check Type", "Check Inheritance", "Check interface" experience.
Another point to note is that the Microsoft AJAX Library was developed based on an open architecture, not just for ASP.net, but also for other architectures, such as Java.

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.