
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Constructor Functions in Subclasses
|
197
// Here's the problematic constructor...
public function ColoredBall (color:Number) {
// OOPs! No call to super( ), so all ColoredBalls
// will start with no radius!
c = color;
}
}
Wait a minute. Doesn’t the compiler automatically supply the call to super( )? Yes, it
does; however, the implicit call to super( ) doesn’t include the necessary
radius
parameter.
As written, the ColoredBall constructor does not allow us to create a ColoredBall
instance with an initial radius defined. Therefore, the ColoredBall constructor must
not call only super( ), but it must also accept
radius as a parameter and pass it on to
the superclass constructor. Here’s the corrected version of ColoredBall, which pre-
serves the Ball constructor’s behavior:
class ColoredBall extends Ball {
private var c:Number;
// All fixed up...
public function ColoredBall (radius:Number, color:Number) {
super(radius);
c = color;
}
}
Notice that, as a matter of good form, we list the superclass’s constructor arguments
first (in this case, the radius), then the additional subclass constructor arguments (in
this case, the color).
Extraneous Superclass Constructor Invocation
in Flash Player 6
Due to the historical architecture of ActionScript 1.0, in Flash Player 6–format .swf
files, a class’s ...