Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 152

Programming actionscript 3.0
Table of Contents

Advertisement

{
// The formula is Pi * radius^2.
return Math.PI * ((diameter / 2)^2);
}
public function getCircumference():Number
{
// The formula is Pi * radius * 2.
return Math.PI * diameter;
}
public function describe():String
{
var desc:String = "This shape is a Circle.\n";
desc += "Its diameter is " + diameter + " pixels.\n";
desc += "Its area is " + getArea() + ".\n";
desc += "Its circumference is " + getCircumference() + ".\n";
return desc;
}
}
}
The Circle class implements the IGeometricShape intrface, so it must provide code for both
the
method and the
getArea()
getCircumference()
declares a property,
diameter
The other two types of shapes, squares and equilateral triangles, have some other things in
common: they each have sides of equal length, and there are common formulas you can use to
calculate the perimeter and sum of interior angles for both. In fact, those common formulas
will apply to any other regular polygons that you need to define in the future as well.
The RegularPolygon class will be the superclass for both the Square class and the
EquilateralTriangle class. A superclass lets you define common methods in one place, so you
don't have to define them separately in each subclass. Here is the code for the RegularPolygon
class:
package com.example.programmingas3.geometricshapes
{
public class RegularPolygon implements IPolygon
{
public var numSides:int;
public var sideLength:Number;
public function RegularPolygon(len:Number = 100, sides:int = 3):void
{
this.sideLength = len;
this.numSides = sides;
}
152
Object-Oriented Programming in ActionScript
describe()
method, which is unique to the Circle class. The Circle class also
, which won't be found in the other polygon classes.
method. In addition, it defines the

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents