User Guide
28 Using ActionScript to Create Components
Using the class statement
You use the class statement to define your class name, and to specify its superclass, as the
following example shows:
package myComponents
{
// Import necessary classes
import mx.core.Container;
import mx.controls.Button;
// Import all classes in the mx.events package
import mx.events.*;
// Class definition goes here.
public class MyButton extends Button {
// Define properties, constructor, and methods.
}
}
The class definition of your component must be prefixed by the public keyword, or it cannot
be used as an MXML tag. A file that contains a class definition can have one, and only one,
public class definition, although it can have additional private class definitions.
In a single ActionScript file, you can define only one class in the package. To define more than
one class in a file, define the additional classes outside of the package body.
Defining the constructor
An ActionScript class must define a public constructor method, which initializes an instance
of the class. The constructor has the following characteristics:
■ No return type.
■ Should be declared public.
■ Might have optional arguments.
■ Cannot have any required arguments if you use it as an MXML tag.
■ Calls the super() method to invoke the superclass’s constructor.
NOTE
The class definition is one of the few ActionScript constructs that you cannot use in an
<mx:Script> block in an MXML file.