Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Inheritance > 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.
For example, you might start with a class called Mammal that has play() and sleep() methods. You then create Cat, Monkey, and Dog subclasses to extend the Mammal class. The subclasses override the play() method from the Mammal class to reflect the habits of those particular kinds of animals. Monkey implements the play() method to swing from trees; Cat implements the play() method to pounce at a ball of yarn; Dog implements the play() method to fetch a ball. Because the sleep() functionality is similar among the animals, you would use the superclass implementation. The following procedure demonstrates this example in Flash.
This document is the base class for a few different animal classes that you create in upcoming steps.
class Mammal {
private var _gender:String;
private var _name:String = "Mammal";
// constructor
public function Mammal(gender:String) {
this._gender = gender;
}
public function toString():String {
return "[object " + speciesName + "]";
}
public function play():String {
return "Chase another of my kind.";
}
public function sleep():String {
return "Close eyes.";
}
public function get gender():String {
return this._gender;
}
public function get speciesName():String {
return this._name;
}
public function set speciesName(value:String):Void {
this._name = value;
}
}
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.
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).
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.
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.
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.
var mammals_arr:Array = new Array();
this.createTextField("info_txt", 10, 10, 10, 450, 80);
info_txt.html = true;
info_txt.multiline = true;
info_txt.border = true;
info_txt.wordWrap = true;
createMammals()
createReport()
function createMammals():Void {
mammals_arr.push(new Dog("Female"));
mammals_arr.push(new Cat("Male"));
mammals_arr.push(new Monkey("Female"));
mammals_arr.push(new Mammal("Male"));
}
function createReport():Void {
var i:Number;
var len:Number = mammals_arr.length;
// Display Mammal info in 4 columns of HTML text using tab stops.
info_txt.htmlText = "<textformat tabstops='[110, 200, 300]'>";
info_txt.htmlText += "<b>Mammal\tGender\tSleep\tPlay</b>";
for (i = 0; i < len; i++) {
info_txt.htmlText += "<p>" + mammals_arr[i].speciesName
+ "\t" + mammals_arr[i].gender
+ "\t" + mammals_arr[i].sleep()
+ "\t" + mammals_arr[i].play() + "</p>";
// The trace statement calls the Mammal.toString() method.
trace(mammals_arr[i]);
}
info_txt.htmlText += "</textformat>";
}
The mammalTest.fla code is a bit more complex than the previous classes. First it imports the three animal classes.
You see the Mammal information displayed in a text field on the Stage, and the following text in the Output panel:
[object Dog] [object Cat] [object Monkey] [object Mammal]
Flash CS3