Classes and ASP.NET AJAX - Accessing base methods
(Page 2 of 5 )
When we talk about class inheritance, a logical question is whether methods can be overridden in derived classes. The answer is yes. The next question: is there any way to access the equivalent method of the base class, (i.e., the overridden method)? Even better, the answer is again yes, ASP.NET AJAX allows you to do so. To demonstrate this, let’s add a toString() method to OReilly.Softwarethat outputs the product and vendor names stored by the class. Theprototypeproperty ensures automated inheritance and also helps demonstrate access to the base method later on.
OReilly.Software.prototype.toString = function() {
return this.getName() + " from " + this.getVendor();
}
You could also directly access the properties_nameand_vendoras variables. Using the getter methods is just a personal preference. There is no functional difference in doing so.
In theOReilly.Browserclass, you could write a similartoString()method:
OReilly.Browser.prototype.toString = function(){
return this.getName() + " from " +
this.getVendor() +
(this.getIsJavaScriptSupported() ? " (w/ JS)" : " (w/o JS)");
}
However, it is once again advisable to reuse existing code, in this case, the base class’stoString()method. ASP.NET AJAX provides you withcallBaseMethod(), a helper method to call a method from the parent class that can take up to three parameters:
instance
The instance whose parent’s method to call (usually
this)
methodName
The name of the method (as a string)
baseArguments
Parameters for the method, if any (as an array)
In this case, thetoString()method ofOReilly.Browsercan be implemented as the following code demonstrates:
OReilly.Browser.prototype.toString = function() {
return OReilly.Browser.callBaseMethod(this, "toString") +
(this.getIsJavaScriptSupported() ? " (w/ JS)" : " (w/o JS)");
}
Now, the code to output the browser information can be reduced a bit to these commands below:
var s = "";
var ie = new OReilly.Browser("Internet Explorer", "Microsoft", true);
s = ie.toString() + "<br />";
var lynx = new OReilly.Browser("Lynx", null, false);
s += lynx.toString();
document.getElementById("output").innerHTML = s;
Example 4-3 shows the complete listing.
Example 4-3. Accessing a base class method
ClientBaseMethods.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server">
<title>ASP.NET AJAX</title>
<script language="Javascript" type="text/javascript">
function pageLoad() {
var s = "";
Type.registerNamespace("OReilly");
OReilly.Software = function(name, vendor) {
var _name = (name != null) ? name : "unknown";
var _vendor = (vendor != null) ? vendor : "unknown";
this.getName = function() {
return _name;
}
this.setName = function(name) {
_name = name;
}
this.getVendor = function() {
return _vendor;
}
this.setVendor = function(vendor) {
_vendor = vendor;
}
}
Type.registerClass("OReilly.Software");
OReilly.Browser = function(name, vendor, isJavaScriptSupported) {
OReilly.Browser.initializeBase(this, new Array(name, vendor));
var _isJavaScriptSupported = (isJavaScriptSupported != null) ?
isJavaScriptSupported : false;
this.getIsJavaScriptSupported = function() {
return _isJavaScriptSupported;
}
this.setIsJavaScriptSupported = function(isJavaScriptSupported) {
_isJavaScriptSupported = isJavaScriptSupported;
}
}
OReilly.Browser.registerClass("OReilly.Browser", OReilly.Software);
OReilly.Software.prototype.toString = function() {
return this.getName() + " from " + this.getVendor();
}
OReilly.Browser.prototype.toString = function() {
return OReilly.Browser.callBaseMethod(this, "toString") +
(this.getIsJavaScriptSupported() ? " (w/ JS)" : " (w/o JS)");
};
var ie = new OReilly.Browser("Internet Explorer", "Microsoft", true);
s = ie.toString() + "<br />";
var lynx = new OReilly.Browser("Lynx", null, false);
s += lynx.toString();
document.getElementById("output").innerHTML = s;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="output">
</div>
</form>
</body>
</html>
Note that when you run this page, the output of this code is identical to that shown in Figure4-2.
Next: Interfaces >>
More ASP.NET Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of Programming ASP.NET AJAX, written by Christian Wenz (O'Reilly, 2007; ISBN: 0596514247). Check it out today at your favorite bookstore. Buy this book now.
|
|