Creating and using classes
55
return "Hello world";
}
}
This rule applies only to instance variables (variables that are copied into each instance of a class),
not class variables (variables that belong to the class). For more information about these kinds of
variables, see
“Instance and class members” on page 60
.
When you initialize arrays inline, only one array is created for all instances of the class:
class Bar {
var foo:Array = new Array();
}
Creating subclasses
In object-oriented programming, a subclass can inherit the properties and methods of another
class, called the superclass. To create this kind of relationship between two classes, you use the
class
statement’s
extends
clause. To specify a superclass, use the following syntax:
class
SubClass
extends
SuperClass
{}
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 53
.)
You can extend your own custom classes as well as many of the built-in ActionScript classes. (You
cannot extend the TextField class or static classes, such as the Math, Key, and Mouse classes.)
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):Void {
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.
Содержание FLEX-FLEX ACTIONSCRIPT LANGUAGE
Страница 1: ...Flex ActionScript Language Reference...
Страница 8: ......
Страница 66: ...66 Chapter 2 Creating Custom Classes with ActionScript 2 0...
Страница 76: ......
Страница 133: ...break 133 See also for for in do while while switch case continue throw try catch finally...
Страница 135: ...case 135 See also break default strict equality switch...
Страница 146: ...146 Chapter 5 ActionScript Core Language Elements See also break continue while...
Страница 229: ...while 229 i 3 The following result is written to the log file 0 3 6 9 12 15 18 See also do while continue for for in...
Страница 808: ...808 Chapter 7 ActionScript for Flash...
Страница 810: ...810 Appendix A Deprecated Flash 4 operators...
Страница 815: ...Other keys 815 Num Lock 144 186 187 _ 189 191 192 219 220 221 222 Key Key code...
Страница 816: ...816 Appendix B Keyboard Keys and Key Code Values...
Страница 822: ...822 Index...