About writing subclasses in Flash 307
3. Save your changes to the ActionScript document.
The Widget class now defines a constructor and a public method called
doSomething().
4. Create a new ActionScript file named SubWidget.as and save it in the same directory
as Widget.as.
5. In SubWidget.as, type the following ActionScript code into the Script window:
class SubWidget extends Widget {
public function SubWidget() {
trace("Creating subwidget # " + Widget.widgetCount);
doSomething();
}
}
6.
Save your changes to SubWidget.as.
Notice that the SubWidget class’s constructor calls the
doSomething() method that you
defined in the superclass.
7. Create a new Flash document and save it as subWidgetTest.fla in the same directory as the
ActionScript documents.
8. In subWidgetTest.fla, type the following ActionScript into Frame 1 of the main Timeline:
var sw1:SubWidget = new SubWidget();
var sw2:SubWidget = new SubWidget();
9.
Save your changes to the Flash document.
10. Select Control > Test Movie to test the Flash document. You see the following output in
the Output panel:
Creating subwidget # 1
Widget::doSomething()
Creating subwidget # 2
Widget::doSomething()
This output shows that the SubWidget class’s constructor calls the constructor of its
superclass (Widget), which increments the static
widgetCount property. The SubWidget’s
constructor traces the superclass’s static property and calls the
doSomething() method,
which inherits from the superclass.
NOTE
If you created the SubWidget class in “Example: Extending the Widget class”
on page 304, you can use this file instead.