154
Chapter 5: ActionScript Core Language Elements
The following example shows a second AS file, called Car.as, in the same directory. This class
extends the Vehicle class, modifying it in three ways. First, the Car class adds a variable
fullSizeSpare
to track whether the car object has a full-size spare tire. Second, it adds a new
method specific to cars,
activateCarAlarm()
, that activates the car’s anti-theft alarm. Third, it
overrides the
stop()
function to add the fact that the Car class uses an anti-lock braking system
to stop.
class Car extends Vehicle {
var fullSizeSpare:Boolean;
function Car(param_numDoors:Number, param_color:String,
param_fullSizeSpare:Boolean) {
this.numDoors = param_numDoors;
this.color = param_color;
this.fullSizeSpare = param_fullSizeSpare;
}
function activateCarAlarm():Void {
trace("[Car] activateCarAlarm");
}
function stop():Void {
trace("[Car] stop with anti-lock brakes");
}
}
The following example instantiates a Car object, calls a method defined in the Vehicle class
(
start()
), then calls the method overridden by the Car class (
stop()
), and finally calls a method
from the Car class (
activateCarAlarm()
):
var myNewCar:Car = new Car(2, "Red", true);
myNewCar.start(); // output: [Vehicle] start
myNewCar.stop(); // output: [Car] stop with anti-lock brakes
myNewCar.activateCarAlarm(); // output: [Car] activateCarAlarm
A subclass of the Vehicle class can also be written using the keyword
super
, which the subclass
can use to access properties and methods of the superclass. The following example shows a third
AS file, called Truck.as, again in the same directory. The Truck class uses the
super
keyword in
the constructor and again in the overridden
reverse()
function.
class Truck extends Vehicle {
var numWheels:Number;
function Truck(param_numDoors:Number, param_color:String,
param_numWheels:Number) {
super(param_numDoors, param_color);
this.numWheels = param_numWheels;
}
function reverse():Void {
beep();
super.reverse();
}
function beep():Void {
trace("[Truck] make beeping sound");
}
}
Summary of Contents for FLEX-FLEX ACTIONSCRIPT LANGUAGE
Page 1: ...Flex ActionScript Language Reference...
Page 8: ......
Page 66: ...66 Chapter 2 Creating Custom Classes with ActionScript 2 0...
Page 76: ......
Page 133: ...break 133 See also for for in do while while switch case continue throw try catch finally...
Page 135: ...case 135 See also break default strict equality switch...
Page 146: ...146 Chapter 5 ActionScript Core Language Elements See also break continue while...
Page 808: ...808 Chapter 7 ActionScript for Flash...
Page 810: ...810 Appendix A Deprecated Flash 4 operators...
Page 815: ...Other keys 815 Num Lock 144 186 187 _ 189 191 192 219 220 221 222 Key Key code...
Page 816: ...816 Appendix B Keyboard Keys and Key Code Values...
Page 822: ...822 Index...