User Guide
108 Object-Oriented Programming in ActionScript
As far back as ActionScript 1.0, ActionScript programmers could use Function objects to
create constructs that resembled classes. ActionScript 2.0 added formal support for classes
with keywords such as
class and extends. ActionScript 3.0 not only continues to support
the keywords introduced in ActionScript 2.0, but also adds some new capabilities, such as
enhanced access control with the
protected and internal attributes, and better control over
inheritance with the
final and override keywords.
If you have ever created classes in programming languages like Java, C++, or C#, you will find
that ActionScript provides a familiar experience. ActionScript shares many of the same
keywords and attribute names, such as
class, extends, and public, all of which are
discussed in the following sections.
Class definitions
ActionScript 3.0 class definitions use syntax that is similar to that used in ActionScript 2.0
class definitions. Proper syntax for a class definition calls for the
class keyword followed by
the class name. The class body, which is enclosed by curly braces(
{}), follows the class name.
For example, the following code creates a class named Shape that contains one variable,
named
visible:
public class Shape
{
var visible:Boolean = true;
}
One significant syntax change involves class definitions that are inside a package. In
ActionScript 2.0, if a class is inside a package, the package name must be included in the class
declaration. In ActionScript 3.0, which introduces the
package statement, the package name
must be included in the package declaration instead of in the class declaration. For example,
the following class declarations show how the BitmapData class, which is part of the
flash.display package, is defined in ActionScript 2.0 and ActionScript 3.0:
// ActionScript 2.0
class flash.display.BitmapData {}
// ActionScript 3.0
package flash.display
{
NOTE
In this chapter, the term property means any member of an object or class, including
variables, constants, and methods. In addition, although the terms class and static are
often used interchangeably, in this chapter these terms are distinct. For example, in this
chapter the phrase class properties refers to all the members of a class, rather than only
the static members.