As discussed briefly in Using classes: a simple example, a class consists of two parts: the declaration and the body. The class declaration consists minimally of the class
statement, followed by an identifier for the class name, then left and right curly braces. Everything inside the curly braces is the class body.
class className
{
// class body
}
Classes can only be defined in external ActionScript (AS) files. For example, you can't define a class on a frame script in a FLA file. Also, the specified className
must match the name of the AS file that contains it. For example, say you've create a class called Shape. The AS file that contains the class definition must be named Shape.as.
// In file Shape.as class Shape { // Shape class body }
Also, all AS class files you create must be saved in one of the designated classpath folders, or folders where Flash looks for class definitions when compiling scripts.
In object-oriented programming, a class, called a subclass, can inherit the properties and methods of another class, called the superclass. To create this kind of relationship between two classes, you use the class
statement's extends
clause. The general form for specifying a superclass is shown below.
class SubClass extends SuperClass {}
The class specified by SubClass inherits all the properties and methods defined by the super class. For example, you might create a Mammal class that defines properties and method common to all mammals. To create variation of the Mammal class, like a Marsupial class, you would subclass, or extend, the Mammal class.
class Marsupial extends Mammal {}
The subclass inherits all the properties and methods of the superclass (unless the superclass has restricted access to certain properties or methods using a private modifier. For more information on private variables see "Static, public, and private members" on page 140.)
Multiple-inheritance, or inheriting from more than one class is not allowed. A class can only extend a single super class. For more information on inheritance see Managing inheritance.