Four local variables
The most interesting thing in Listing 4 is the declaration and population of the following four local variables:
- randomChoice
- radius
- width
- height
A random value
The variable named randomChoice is of type Number and is populated with the return value from a call to the random method of the Math class. The documentation tells us that this method:
"Returns a pseudo-random number n, where 0 LTE n LT 1. The number returned is calculated in an undisclosed manner, and pseudo-random because thecalculation inevitably contains some element of non-randomness."
Note that LTE and LT represent "less than or equal" and "less than" respectively. (Lots of problems arise from including angle brackets and ampersands in html text so I avoid doing that when I can.)
A fractional random value
Thus, the return value is a fractional value that is greater than or equal to zero and less than 1.0. This value will be used later to decide which typeof object to instantiate.
Three more random values
The last three variables in the above list are also populated with random values, but not with fractional values in the rangefrom 0 to 0.999... Instead, these variables are populated with unsigned integer values in the range of 1 to 10 inclusive. This is accomplished bymultiplying the original random value by 10, adding 1 to that product, and casting the result to an unsigned integer.
A local variable of type MyShape
Listing 5 begins by declaring a variable of type MyShape that will be used to hold a reference to an object instantiated from one of the followingclasses: MyShape , MyCircle , or MyRectangle .
Instantiate an object.
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);}//end else
Instantiate an object
Then Listing 5 uses the random value stored in the variable named randomChoice to make a decision and to instantiate an object of one of the three classes listed above . The decision as to which type of object to instantiate and store in the variable named myShape is completely random and completely unknown to the compiler when the program iscompiled.
Random values for radius, width, and height
Note also that Listing 5 also uses the other values stored in the other variables in the above list to specify the radius of the circle, or to specify the width and the height of the rectangle.
Call the area method on the object
Finally, (and this is the essence of runtime polymorphism) , Listing 6 calls the area method on the randomly instantiated object and writes the return value into the text area shown in Figure 2.
Call the area method on the object.
textArea.text = myShape.area();
}//end buttonHandler}//end class
}//end package
And the result will be...
The result will be similar to Figure 3, Figure 4, or Figure 5, depending on which type of object is instantiated and depending on the random values passedfor radius, width, and height.