Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 153

Programming actionscript 3.0
Table of Contents

Advertisement

public function getArea():Number
{
// This method should be overridden in subclasses.
return 0;
}
public function getPerimeter():Number
{
return sideLength * numSides;
}
public function getSumOfAngles():Number
{
if (numSides >= 3)
{
return ((numSides - 2) * 180);
}
else
{
return 0;
}
}
public function describe():String
{
var desc:String = "Each side is " + sideLength + " pixels long.\n";
desc += "Its area is " + getArea() + " pixels square.\n";
desc += "Its perimeter is " + getPerimeter() + " pixels long.\n";
desc += "The sum of all interior angles in this shape is " +
getSumOfAngles() + " degrees.\n";
return desc;
}
}
}
First, the RegularPolygon class declares two properties that are common to all regular
polygons: the length of each side (the
property).
numSides
The RegularPolygon class implements the IPolygon interface and declares all four of the
IPolygon interface methods. It implements two of these—the
methods—using common formulas.
getSumOfAngles()
Because the formula for the
version of the method cannot include common logic that can be inherited by the subclass
methods. Instead, it simply returns a 0 default value to indicate that the area was not
calculated. To calculate the area of each shape correctly, the subclasses of the RegularPolygon
class will have to override the
sideLength
method will differ from shape to shape, the base class
getArea()
method themselves.
getArea()
property) and the number of sides (the
getPerimeter()
Example: GeometricShapes
and
153

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents