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.