310 Inheritance
The previous class defines two private variables, _gender and _name, which are used to
store the animal’s gender and mammal type. Next, the
Mammal constructor is defined. The
constructor takes a single parameter,
gender, which it uses to set the private _gender
variable defined earlier. Three additional public methods are also specified:
toString(),
play(), and sleep(), each of which returns string objects. The final three methods are
getter and setter methods for the mammal’s
_gender and _name properties.
3. Save the ActionScript document.
This class serves as the superclass for the Cat, Dog, and Monkey classes, which you create
shortly. You can use the
toString() method of the Mammal class to display a string
representation of any Mammal instance (or any instance that extended the
Mammal class).
4. Create a new ActionScript file and save it as Cat.as in the same directory as the Mammal.as
class file you created in step 1.
5. In Cat.as, type the following ActionScript code into the Script window:
class Cat extends Mammal {
// constructor
public function Cat(gender:String) {
super(gender);
speciesName = "Cat";
}
public function play():String {
return "Pounce a ball of yarn.";
}
}
Notice that you are overriding the play() method in the Mammal superclass. The Cat
class defines only two methods, a constructor and a
play() method. Since the Cat class
extends the Mammal class, the Mammal classes’s methods and properties are inherited by
the Cat class. For more information on overriding, see “Overriding methods and
properties” on page 306.
6. Save your changes to the ActionScript document.
7. Create a new ActionScript document and save it as Dog.as in the same directory as the two
previous class files.