<< Chapter < Page Chapter >> Page >

The file named MyRectangle.as

The class named MyRectangle is so similar to the class named MyCircle that it doesn't warrant a detailed explanation. A complete listing of the file is provided in Listing 13 near the end of the lesson.

The constructor receives and saves numeric values for the width and the height of the rectangle.

The overridden area method computes the area as the product of the width and the height and concatenates that information into a returned String object where it is displayed in the format shown in Figure 5.

Run the program

I encourage you to run this program from the web. Then copy the code from Listing 9 through Listing 13. Use thatcode to create a Flex project. Compile and run the project. Experiment with the code, making changes, and observing the results of yourchanges. Make certain that you can explain why your changes behave as theydo.


I will publish a list containing links to ActionScript resources as a separate document. Search for ActionScript Resources in theConnexions search box.

Complete program listing

Complete listings of the Flex and ActionScript files used in this program are provided in Listing 9 through Listing 13 below.

Listing of the file named polymorph02.mxml.

<?xml version="1.0" encoding="utf-8"?><!--Illustrates polymorphism.--><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"xmlns:cc="CustomClasses.*"><cc:Driver/></mx:Application>

Listing of the file named driver.as.

package CustomClasses{ import flash.events.*;import mx.containers.VBox;import mx.controls.Button; import mx.controls.Label;import mx.controls.TextArea;public class Driver extends VBox{ private var textArea:TextArea = new TextArea();public function Driver(){//constructorvar label:Label = new Label(); label.text = "Polymorphism Demonstration";label.setStyle("fontSize",14); label.setStyle("color",0xFFFF00);addChild(label);var button:Button = new Button(); button.label = "GO";addChild(button);textArea.width = 200; textArea.height = 70;addChild(textArea);button.addEventListener( MouseEvent.CLICK,buttonHandler);}//end constructorprivate function buttonHandler( event:MouseEvent):void{var randomChoice:Number = Math.random(); var radius:uint = uint(10*Math.random() + 1);var width:uint = uint(10*Math.random() + 1); var height:uint = uint(10*Math.random() + 1);var myShape:MyShape;if(randomChoice<0.33333){ myShape = new MyShape();}else if(randomChoice<0.66666){ myShape = new MyCircle(radius);}else{ myShape = new MyRectangle(width,height);} textArea.text = myShape.area();}//end buttonHandler }//end class}//end package

Listing of the file named myshape.as.

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

Listing of the file named mycircle.as.

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

Listing of the file named myrectangle.as

package CustomClasses{ public class MyRectangle extends MyShape{private var width:Number; private var height:Number;public function MyRectangle(width:Number,height:Number){//constructor this.width = width;this.height = height; }//end constructoroverride public function area():String{return "Rectangle\n" + "Width = " + width + "\n" +"Height = " + height + "\n" + "Area = " + width * height;}//end area }//end class}//end package


This section contains a variety of miscellaneous materials.

Housekeeping material
  • Module name: Polymorphism - The Big Picture
  • Files:
    • ActionScript0110\ActionScript0110.htm
    • ActionScript0110\Connexions\ActionScriptXhtml0110.htm
PDF disclaimer: Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.


Questions & Answers

give me somes of examples.
Manu Reply
give example of what.
example of what
Ecology is the study of
Abdul Reply
Ecology is the study of the interaction of organism in each other and their environment.
Ecology is the study of the interaction of organism in each other and their environment
oh yes!!! exactly
thank much
what is biome?
a geographical area with dinstic species
thanks much it very helpful
my pleasure
characteristics of living things
Jane Reply
excretion, movement, sensitivity, reproduction,growth, respiration,nutrition
what are there function
what are their function
what is the two type of reproduction?
sexual reproduction and asexual reproduction
what are their function
What is light independent reaction
Benjamin Reply
correct word or phrase. ______A____ uses carbon dioxide and _____B______ to make sugar during______C______. _____D______ traps light energy during this process. *
Andrea Reply
A_plant ,B_water,C_photosynthesis,D_chlorophyll
by which of the following feature c3 and c4 plant similar A .enzyme used to fix carbon dioxide B.cell where calvin cycle takes place C.cell where carbon dioxide fixation takes place
Kaleab Reply
correct word or phrase. ______A____ uses carbon dioxide and _____B______ to make sugar during______C______. _____D______ traps light energy during this process. *
what is biology ?
Bizone Reply
study of organisms
what is link
Lamina Reply
is carbon an organism that matter?
DKP Reply
what is buttress root?
Dora Reply
Buttress root are wide large root
Buttress root/roots are thick roots that emerge out form the base of a large canopy
From the base of a large canopy
What is biology
Jiboh Reply
Biology is the study of structure and growth of living things and organisms within their biography
compare the mechanism of gaseous exchange in an insect and mammal
World Reply
what are the characteristic of livingthing
Joy Reply
Movement Respiration Nutrition Irritability or sensitivity Growth Excretion Reproduction Adaptation Competition Death or Life Span
Respiration Irritability Movement Excretion Nutrition Growth Reproduction
movement respiration nutrition irritability growth excretion reproduction death
what is the function of medulla oblongata
mbalenhle Reply
what is the strongest bone in the human body
what is biology
Dauda Reply
is a branch of science which deals with the study of living thing
Biology is got from two main words: bios: means life and Logos: means knowledge therefore, biology is a branch of science that deals with knowledge, life and functioning of living things.
what are the characteristics of organism
Responds to stimuli
Biology is an aspect of science that deals with the. study of living organisms
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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?