<< Chapter < Page | Chapter >> Page > |
You shouldn't find any surprises in Listing 2. The code in Listing 2 simply constructs the GUI shown in Figure 2, placing the label, the buttons, andthe text area in their respective locations.
The remainder of the constructor for the Driver class
The remainder of the constructor for the Driver class is shown in Listing 3.
//Register a click event handler on each of the
// buttonsareaButton.addEventListener(
MouseEvent.CLICK,areaButtonHandler);circButton.addEventListener(MouseEvent.CLICK,circButtonHandler);volumeButton.addEventListener(
MouseEvent.CLICK,volumeButtonHandler);}//end constructor
Listing 3 registers a click event handler on each of the buttons shown in Figure 2.
The getRandomValues method
The method named getRandomValues is shown in Listing 4.
//Local utility method for getting and saving four
// random values.private function getRandomValues():void{
randomChoice = Math.random();radius = uint(10*Math.random() + 1);
rectWidth = uint(10*Math.random() + 1);rectHeight = uint(10*Math.random() + 1);
}//end getRandomValues
This method is called by each of the event handler methods to get and save four random values that are subsequently used by the event handler. Therandom values are saved in the instance variables that are declared in Listing 1.
The event handler method named areaButtonHandler
The click event handler that is registered on the button labeled Area in Figure 2 is shown in Listing 5.
//Define click event handler methods.
private function areaButtonHandler(event:MouseEvent):void{getRandomValues();if(randomChoice<0.5){
myShape = new MyCircle(radius);}else{
myShape = new MyRectangle(rectWidth,rectHeight);}//end elsetextArea.text = myShape.id() + myShape.area();
}//end areaButtonHandler
Decide between two classes
The code in Listing 5 uses the random value stored in randomChoice to decide whether to instantiate an object of the class named MyCircle or the class named MyRectangle .
Store object's reference as type IArea
The object's reference is stored in the instance variable named myShape , which is type IArea . Storage of the reference in a variable of that type is possible only because both objects implement the interface named IArea .
Call the id method using the reference
Then the code in Listing 5 uses the reference stored in myShape to call the id method and the area method on the object. The returned values are used to construct a new text string for the text area shownin Figure 3.
How is this possible?
Calling these methods using the reference of type IArea is possible only because abstract versions of both methods are inherited into both classesfrom the interface named IArea .
The other two click event handler methods
The click event handler methods that are registered on the Circumference button and the Volume button in Figure 2 are shown in Listing 6.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?