208
ActionScript language elements
Example
In the following example, the Car class extends the Vehicle class so that all of its methods,
properties, and functions are inherited. If your script instantiates a Car object, methods from
both the Car class and the Vehicle class can be used.
The following example shows the contents of a file called Vehicle.as, which defines the Vehicle
class:
class Vehicle {
var numDoors:Number;
var color:String;
function Vehicle(param_numDoors:Number, param_color:String) {
this.numDoors = param_numDoors;
this.color = param_color;
}
function start():Void {
trace("[Vehicle] start");
}
function stop():Void {
trace("[Vehicle] stop");
}
function reverse():Void {
trace("[Vehicle] reverse");
}
}
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()
, which activates the car's antitheft alarm.
Third, it overrides the
stop()
function to add the fact that the Car class uses an antilock
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");
}
}
Содержание FLASH 8-ACTIONSCRIPT 2.0 LANGUAGE
Страница 1: ...ActionScript 2 0 Language Reference ...
Страница 1352: ...1352 ActionScript classes ...