<< Chapter < Page Chapter >> Page >

The end of the class

Listing 6 also signals the end of the class named Driver .

The file named MyShape.as

The class named MyShape is shown in its entirety in Listing 7, and also in Listing 11 near the end of the lesson.

The class named myshape.

package CustomClasses{ public class MyShape{public function area():String{ return "General Shape\n" +"Unable to compute area."; }// end area method}//end class}//end package

The sole purpose

The sole purpose of the class named MyShape is to serve as the root of a hierarchy of more specialized shape classes and to provide a default versionof the area method that can be overridden in the subclasses.

The class accomplishes those things well, and beyond that, there isn't much to say about the code in Listing 7. Note that the class doesn't evendefine a constructor but instead uses the default constructor that is provided by the compiler.

The file named MyCircle.as

The class named MyCircle is shown in its entirety in Listing 8, and also in Listing 12 near the end of the lesson.

The class named mycircle.

package CustomClasses{ public class MyCircle extends MyShape{private var radius:Number;public function MyCircle(radius:Number){//constructor this.radius = radius;}//end constructoroverride public function area():String{ return "Circle\n" +"Radius = " + radius + "\n" + "Area = " + Math.PI * radius * radius;}//end area }//end class}//end package

The class named MyCircle is only slightly more complicated than the class named MyShape .

The constructor

The constructor for the class named MyCircle receives an incoming value for the radius and saves that value in a private instance variable named radius . Saving the value in an instance variable makes it available to the area method that will be executed later.

Type coercion

Recall from Listing 4 and Listing 5 that the value that is actually passed to the constructor is of type uint , which is an unsigned integer. That value is coerced to type Number by passing it as an argument that is defined to be of type Number .

An overridden area method

Note that the method named area is declared to be an override. This syntax is required by ActionScript when a method in a subclass overrides an inheritedmethod.

Return a String object

The code in the area method concatenates several individual strings (including the computed area of the circle) into a single String object and returns a reference to that object. A sample of the returned value is shown displayed in the text area of Figure 4.

Concatenation of strings with numeric values

The computed value of the area and the stored value of the radius are both numeric values. When a numeric value is concatenated with a string, thenumeric value is coerced into a string of characters, decimal points, etc., and the two strings are concatenated into a single string.

Computation of the area

As you can see in Listing 8, the area of the circle is computed as the square of the radius multiplied by the constant PI. (Hopefully you recall that formula from your high school geometry class.) As mentioned earlier, the resulting area value is concatenated with the string to its left where itbecomes part of the larger String object that is returned by the method.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask