Using polymorphism in an application
311
8.
In Dog.as, type the following ActionScript code into the Script window:
class Dog extends Mammal {
// constructor
public function Dog(gender:String) {
super(gender);
speciesName = "Dog";
}
public function play():String {
return "Fetch a stick.";
}
}
Notice that the Dog class is very similar in structure to the Cat class, except that a few of
the values have changed. Again, the Dog class extends the Mammal class and inherits all
its methods and properties. The Dog constructor takes a single property, gender, which it
passes to the Dog class’s parent class, Mammal. The
speciesName
variable is also
overridden and set to the string
Dog
. The
play()
method is also overridden from the
parent class.
9.
Save your changes to the ActionScript document.
10.
Create another ActionScript document in the same directory as your other files, and save
it as
Monkey.as
.
11.
In Monkey.as, type the following ActionScript code into the Script window:
class Monkey extends Mammal {
// constructor
public function Monkey(gender:String) {
super(gender);
speciesName = "Monkey";
}
public function play():String {
return "Swing from a tree.";
}
}
Similar to the previous two classes, Cat and Dog, the Monkey class extends the Mammal
class. The Monkey class’s constructor calls the constructor for the Mammal class, passing
the gender to the Mammal’s constructor, as well as setting
speciesName
to the string
Monkey
. The Monkey class also overrides the behavior of the
play()
method.
12.
Save your changes to the ActionScript document.
13.
Now that you’ve created three subclasses of the Mammal class, create a new Flash
document called
mammalTest.fla
.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...