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.
Содержание 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...