EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 309

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...
Using polymorphism in an application 309
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.
To use polymorphism in an application:
1. Create a new ActionScript document and save it as Mammal.as.
This document is the base class for a few different animal classes that you create in
upcoming steps.
2. In Mammal.as, type the following ActionScript code into the Script window:
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;
}
}

Table of Contents

Related product manuals