Creating and using classes
163
The class you specify in
SubClass
inherits all the properties and methods defined by the
superclass. For example, you might create a Mammal class that defines properties and methods
common to all mammals. To create a variation of the Mammal class, such as a Marsupial class,
you would extend the Mammal class—that is, create a subclass of the Mammal class.
class Marsupial extends Mammal {}
The subclass inherits all the properties and methods of the superclass, including any properties or
methods that you have declared to be private using the
private
keyword. (For more information
on private variables, see
“Controlling member access” on page 164
.)
You can extend your own custom classes, as well as any of the built-in ActionScript classes, such as
the XML, Sound, or MovieClip class. When you extend a built-in ActionScript class, your
custom class inherits all the methods and properties of the built-in class.
For example, the following code defines the class JukeBox, which extends the built-in Sound class.
It defines an array called
songList
and a method called
playSong()
that plays a song and
invokes the
loadSound()
method, which it inherits from the Sound class.
class JukeBox extends Sound {
var songList:Array = new Array("beethoven.mp3", "bach.mp3", "mozart.mp3");
function playSong(songID:Number) {
this.loadSound(songList[songID]);
}
}
If you don’t place a call to
super()
in the constructor function of a subclass, the compiler
automatically generates a call to the constructor of its immediate superclass with no parameters as
the first statement of the function. If the superclass doesn’t have a constructor, the compiler
creates an empty constructor function and then generates a call to it from the subclass. However,
if the superclass takes parameters in its definition, you must create a constructor in the subclass
and call the superclass with the required parameters.
Multiple inheritance, or inheriting from more than one class, is not allowed. However, classes can
effectively inherit from multiple classes if you use individual
extends
statements:
// not allowed
class C extends A, B {}
// allowed
class B extends A {}
class C extends B {}
You can also use the
extends
keyword to create subclasses of an interface:
interface iA extends interface iB {}
Constructor functions
A class’s
constructor
is a special function that is called automatically when you create an instance of
a class using the
new
operator. The constructor function has the same name as the class that
contains it. For example, the Person class you created earlier contained the following constructor
function:
// Person class constructor function
function Person (myName:String, myAge:Number) {
name = myName;
age = myAge;
}
Summary of Contents for FLASH MX 2004 - ACTIONSCRIPT
Page 1: ...ActionScript Reference Guide...
Page 8: ...8 Contents...
Page 12: ......
Page 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Page 54: ...54 Chapter 2 ActionScript Basics...
Page 80: ...80 Chapter 3 Writing and Debugging Scripts...
Page 82: ......
Page 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Page 112: ......
Page 120: ...120 Chapter 6 Using the Built In Classes...
Page 176: ......
Page 192: ...192 Chapter 10 Working with External Data...
Page 202: ...202 Chapter 11 Working with External Media...
Page 204: ......
Page 782: ...782 Chapter 12 ActionScript Dictionary...
Page 793: ...Other keys 793 221 222 Key Key code...
Page 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Page 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Page 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Page 816: ...816 Index...