Macromedia flex 2 Manual
Using ActionScript
29
You call the
super()
method within your constructor to invoke the superclass’s constructor to
initialize the inherited items from the superclass. The
super()
method should be the first
statement in your constructor; otherwise, the inherited parts of the superclass might not be
properly constructed. In some cases, you might want to initialize your class first, and then call
properly constructed. In some cases, you might want to initialize your class first, and then call
super()
.
In the following example, you define a constructor that uses
super()
to call the superclass’s
constructor:
package myComponents
{
{
// Import necessary classes
import mx.core.Container;
import mx.controls.Button;
// Import all classes in the mx.events package
import mx.events.*;
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 {
public class MyButton extends Button {
// Public constructor.
public function MyButton()
{
public function MyButton()
{
// Call the constructor in the superclass.
super();
super();
}
// Define properties and methods.
}
}
NO
T
E
If you do not define a constructor, the compiler inserts one for you and adds a call to
super()
. However, it is considered a best practice to write a constructor and to explicitly
call
super()
, unless the class contains nothing but static members. If you define the
constructor, but omit the call to
super()
, Flex automatically calls
super()
at the beginning
of your constructor.
NO
TE
You cannot define a constructor for an MXML component. For more information, see