
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
198
|
Chapter 6: Inheritance
Class A has one subclass, B; hence, in a Flash Player 6–format .swf file, class A’s con-
structor executes once, needlessly, even if no instance of A or B is ever created. For
example, if the following code appears in a .fla file, then A’s constructor will run:
var bInstance:B;
Extraneous constructor executions can perform unwanted tasks, such as attaching a
movie clip or incrementing an instance counter. To prevent unwanted code execu-
tion, use a parameter that indicates that the constructor is being called intentionally.
For example:
class A {
public function A (doConstruct:Boolean) {
if (doConstruct) {
// Normal constructor code goes here.
}
}
}
class B extends A {
public function B ( ) {
// Pass true to the constructor to tell it to perform initialization.
super(true);
}
}
This problem does not affect Flash Player 7–format .swf files that use ActionScript 2.0.
For much more information, see Chapter 12 of ActionScript for Flash MX: The Defini-
tive Guide (O’Reilly).
Subclassing Built-in Classes
Just as we can create subclasses of our own custom classes, we can also create sub-
classes of ActionScript’s built-in classes, allowing us to implement specialized func-
tionality based on an existing ActionScript class. For example, suppose we wanted to
implement