ActionScript 3.0 is class-based: a class fixes the properties and methods its instances have, the compiler checks uses of them, and the runtime looks them up by slot rather than by name. A class has one superclass and may implement any number of interfaces.
class
[public | internal] [final] [dynamic] class Name
[extends SuperClass] [implements Interface1, Interface2…] {
[static and instance variables, constants, methods, getters and setters]
[constructor: public function Name(parameters) { … }]
[statements, run once when the class is first set up]
}
package com.example.shapes {
import flash.display.Shape;
public class Circle extends Shape implements IShape {
public static const DEFAULT_RADIUS:Number = 10;
private static var _count:int = 0;
private var _radius:Number;
public function Circle(radius:Number = DEFAULT_RADIUS) {
super();
_radius = radius;
_count++;
draw();
}
public static function get count():int { return _count; }
public function get radius():Number { return _radius; }
public function set radius(value:Number):void {
_radius = value;
draw();
}
public function area():Number {
return Math.PI * _radius * _radius;
}
protected function draw():void {
graphics.clear();
graphics.beginFill(0x3366CC);
graphics.drawCircle(0, 0, _radius);
graphics.endFill();
}
}
}
com/example/shapes/Circle.as: a class with static and instance
members, a getter and setter pair and a protected method.Class attributes
| Attribute | Meaning |
|---|---|
public | Usable from any package. |
internal | Usable only in its own package (the default). |
final | Cannot be extended. |
dynamic | Instances can be given new properties at run time. Not inherited: a subclass of a dynamic class is sealed unless it is dynamic too. |
private and protected cannot be applied to a class, and
there is no abstract: a class meant only to be extended usually throws
an IllegalOperationError from its constructor or from methods subclasses
must override.
Member attributes
| Attribute | On | Meaning |
|---|---|---|
public, internal, protected, private | Any member | Who can see it; see Access attributes. The default is internal. |
| A user-defined namespace | Any member | In place of an access attribute. |
static | Variables, constants, methods, getters, setters | Belongs to the class, reached as ClassName.member, never through an instance. |
override | Methods, getters, setters | Replaces an inherited one. |
final | Methods, getters, setters | Cannot be overridden. |
native | Methods | Implemented by the runtime; only the built-in classes use it. |
Variables and constants
- Instance variables are set up for each new object before the constructor runs, from their initial values in the declaration.
- Static variables and constants are set up once, when the class is initialised. A static constant must get its value in its declaration.
- Statements written directly in the class body, outside any method, run once at that time too, a kind of static initialiser.
- A static member and an instance member may have the same name.
- Variables cannot be overridden by a subclass, and a subclass cannot declare a variable with the name of an inherited one. Getters and setters can be overridden.
- Static members are not inherited, but they are in scope inside the bodies of subclasses, so a subclass can use them without the class name.
Constructors
The constructor is a method with the same name as the class. It may only be
public (the attribute may be left out), has no return type and returns no
value. There is one constructor per class: overloading is not supported, so optional
parameters are used instead. When a class has no constructor, the compiler supplies an
empty one.
super
super(arguments); // in a constructor: run the superclass constructor
super.method(arguments); // in a method: call the superclass version
super.property // read the superclass getter
The superclass constructor always runs. If the constructor does not call
super(), the compiler inserts a call with no arguments at the start, which
fails when the superclass constructor needs arguments. Unlike in ActionScript 2.0 the
call need not be the first statement, though Adobe advises putting it first, and it
must come before any return or throw. super
cannot be used in a static method.
Getters and setters
[attributes] function get name():Type { … }
[attributes] function set name(value:Type):void { … }
A getter and setter pair looks like a variable to the code using it:
circle.radius = 20 calls the setter. A getter alone makes a read-only
property, a setter alone a write-only one. The setter’s return type must be
void or left out, and both halves must use the same type. Unlike variables,
getters and setters can be overridden and can be declared in interfaces.
override and final
A method, getter or setter that replaces an inherited one must say
override, and saying it when there is nothing to replace is an error.
The name, access attribute, number and types of the parameters and the return type
must all match the original. override cannot be used on variables,
constants, static methods, methods that implement an interface, or inherited methods
marked final.
public class FilledCircle extends Circle {
override protected function draw():void {
super.draw();
graphics.lineStyle(2, 0x000000);
graphics.drawCircle(0, 0, radius);
}
}
Sealed and dynamic classes
A class is sealed unless it says dynamic: its instances have exactly the
properties the class declares. Adding one is a compile error in strict mode and a
ReferenceError (1056, “Cannot create property”) at run time. A dynamic
class, such as Object, Array or MovieClip,
accepts new properties; they are untyped, their uses are not checked by the compiler,
and functions stored in them cannot reach the class’s private members.
interface
[public | internal] interface IName [extends IOther1, IOther2…] {
function method(parameters):ReturnType;
function get name():Type;
function set name(value:Type):void;
}
- An interface holds only method, getter and setter signatures: no bodies, no variables, no constants.
- Its members take no access attribute; a class implements them as
publicmethods with exactly the same signature, but withoutoverride. - An interface may extend several interfaces, and a class may implement several.
- An interface is a type: variables, parameters and return values can be typed with it, and
isandastest for it. - By convention interface names start with
I, as inIEventDispatcher.
package com.example.shapes {
public interface IShape {
function area():Number;
function get radius():Number;
}
}
com/example/shapes/IShape.as, which Circle above implements.The prototype object
Every class still has a prototype object, as in ActionScript 1.0. The
methods a class declares are fixed traits, not prototype properties, but properties
added to ClassName.prototype at run time are found on instances that do
not define the name themselves, and the core classes keep their ECMAScript methods
there alongside the AS3
versions. Only dynamic classes benefit in strict mode, since the compiler does not know
prototype properties exist.
Enumerations
The language has no enum. A final class with static
constants serves instead, as the API does with MouseEvent.CLICK or
StageAlign.TOP_LEFT.
package com.example {
public final class Direction {
public static const NORTH:String = "north";
public static const SOUTH:String = "south";
public static const EAST:String = "east";
public static const WEST:String = "west";
}
}
What every object has
Every class inherits from Object: hasOwnProperty(name),
isPrototypeOf(obj), propertyIsEnumerable(name),
setPropertyIsEnumerable(name, flag), toString(),
toLocaleString(), valueOf(), and the
constructor property, which is the object’s class.