EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Using Polymorphism in an Application

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
308 Inheritance
11. Open the SubWidget class and add a new method named doSomething(). Modify your
class so that it matches the following code (add the code that’s in boldface):
class SubWidget extends Widget {
public function SubWidget() {
trace("Creating subwidget # " + Widget.widgetCount);
doSomething();
}
public function doSomething():Void {
trace("SubWidget::doSomething()");
}
}
12.
Save your changes to the class file, and then open subwidgetTest.fla again.
13. Select Control > Test Movie to test the file. You see the following output in the
Output panel:
Creating subwidget # 1
SubWidget::doSomething()
Creating subwidget # 2
SubWidget::doSomething()
The previous output shows that the doSomething() method in the SubWidget classs
constructor is calling the
doSomething() method in the current class instead of
the superclass.
Open the SubWidget class again, and modify the SubWidget classs constructor to call the
superclasss
doSomething() method (add the code that’s in boldface):
public function SubWidget() {
trace("Creating subwidget # " + Widget.widgetCount);
super.doSomething();
}
As demonstrated, you can add the super keyword to call the superclass’s doSomething()
method instead of the
doSomething() method in the current class. For additional
information on
super, see the super entry in the ActionScript 2.0 Language Reference.
14. Save the SubWidget class file with the modified constructor and select Control > Test
Movie to republish the Flash document.
The Output panel displays the contents of the Widget classs
doSomething() method.
Using polymorphism in an application
Object-oriented programming lets you express differences between individual classes using a
technique called polymorphism, by which classes can override methods of their superclasses
and define specialized implementations of those methods.

Table of Contents

Related product manuals