User Guide
Example: GeometricShapes 153
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
sideLength property) and the number of sides (the
numSides property).
The RegularPolygon class implements the IPolygon interface and declares all four of the
IPolygon interface methods. It implements two of these—the
getPerimeter() and
getSumOfAngles() methods—using common formulas.
Because the formula for the
getArea() method will differ from shape to shape, the base class
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
getArea() method themselves.