Using the class statement
You use the
statement to define your class name, and to specify its superclass, as the
class
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
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.
The class definition is one of the few ActionScript constructs that you cannot use in an
block in an MXML file.
<mx:Script>
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()
28
Using ActionScript to Create Components
method to invoke the superclass's constructor.
keyword, or it cannot
public