User Guide

298 Chapter 2: ActionScript Language Reference
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 cars 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
See also
class, implements, interface