15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1236 ❘ ChaPTer 41 Asp.net feAtures<br />

ProCSharp.Circle = function(color, scaleFactor, diameter) {<br />

ProCSharp.Circle.initializeBase(this, [color, scaleFactor]);<br />

this._diameter = diameter;<br />

}<br />

You define properties <strong>and</strong> methods in the same way as before:<br />

ProCSharp.Circle.prototype = {<br />

get_Diameter : function() {<br />

return this._diameter;<br />

},<br />

set_Diameter : function(diameter) {<br />

this._diameter = diameter;<br />

},<br />

get_Area : function() {<br />

return Math.PI * Math.pow((this._diameter * this._scaleFactor) / 2, 2);<br />

},<br />

describe : function() {<br />

var description = "This is a " + this._color + " circle with an area of "<br />

+ this.get_Area();<br />

alert(description);<br />

}<br />

}<br />

When you register the class, you provide the base class type as a second parameter:<br />

ProCSharp.Circle.registerClass('ProCSharp.Circle', ProCSharp.Shape);<br />

You can implement interfaces by passing them as additional parameters, although, to keep things simple,<br />

you won’t see details of that here.<br />

Using User-Defined Types<br />

After you have defined classes in this way, you can instantiate <strong>and</strong> use them with simple syntax.<br />

For example:<br />

var myCircle = new ProCSharp.Circle('red', 1.0, 4.4);<br />

myCircle.describe();<br />

This code would result in a JavaScript alert box, as shown in Figure 41-18.<br />

figure 41-18<br />

If you want to test this, run the PCSLibraryDemo project <strong>and</strong> click the Test OOP Functionality button.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!