Flash Lite 2 |
|||
| Flash Lite 2.x ActionScript Language Reference > ActionScript language elements > Statements > class statement | |||
[dynamic] class className [ extends superClass ] [ implements interfaceName[, interfaceName... ] ] { // class definition here}
Defines a custom class, which lets you instantiate objects that share methods and properties that you define. For example, if you are developing an invoice-tracking system, you could create an invoice class that defines all the methods and properties that each invoice should have. You would then use the new invoice() command to create invoice objects.
The name of the class must match the name of the external file that contains the class. The name of the external file must be the name of the class with the file extension .as appended. For example, if you name a class Student, the file that defines the class must be named Student.as.
If a class is within a package, the class declaration must use the fully qualified class name of the form base.sub1.sub2.MyClass. Also, the class's AS file must be stored within the path in a directory structure that reflects the package structure, such as base/sub1/sub2/MyClass.as. If a class definition is of the form "class MyClass," it is in the default package and the MyClass.as file should be in the top level of some directory in the path.
For this reason, it's good practice to plan your directory structure before you begin creating classes. Otherwise, if you decide to move class files after you create them, you have to modify the class declaration statements to reflect their new location.
You cannot nest class definitions; that is, you cannot define additional classes within a class definition.
To indicate that objects can add and access dynamic properties at runtime, precede the class statement with the dynamic keyword. To declare that a class implements an interface, use the implements keyword. To create subclasses of a class, use the extends keyword. (A class can extend only one class, but can implement several interfaces.) You can use implements and extends in a single statement. The following examples show typical uses of the implements and extends keywords:
class C implements Interface_i, Interface_j // OK class C extends Class_d implements Interface_i, Interface_j // OK class C extends Class_d, Class_e // not OK
Availability: ActionScript 2.0; Flash Lite 2.0
className:String - The fully qualified name of the class.
The following example creates a class called Plant. The Plant constructor takes two parameters.
// Filename Plant.as
class Plant {
// Define property names and types
var leafType:String;
var bloomSeason:String;
// Following line is constructor
// because it has the same name as the class
function Plant(param_leafType:String, param_bloomSeason:String) {
// Assign passed values to properties when new Plant object is created
this.leafType = param_leafType;
this.bloomSeason = param_bloomSeason;
}
// Create methods to return property values, because best practice
// recommends against directly referencing a property of a class
function getLeafType():String {
return leafType;
}
function getBloomSeason():String {
return bloomSeason;
}
}
In an external script file or in the Actions panel, use the new operator to create a Plant object.
var pineTree:Plant = new Plant("Evergreen", "N/A");
// Confirm parameters were passed correctly
trace(pineTree.getLeafType());
trace(pineTree.getBloomSeason());
The following example creates a class called ImageLoader. The ImageLoader constructor takes three parameters.
// Filename ImageLoader.as
class ImageLoader extends MovieClip {
function ImageLoader(image:String, target_mc:MovieClip, init:Object) {
var listenerObject:Object = new Object();
listenerObject.onLoadInit = function(target) {
for (var i in init) {
target[i] = init[i];
}
};
var JPEG_mcl:MovieClipLoader = new MovieClipLoader();
JPEG_mcl.addListener(listenerObject);
JPEG_mcl.loadClip(image, target_mc);
}
}
In an external script file or in the Actions panel, use the new operator to create an ImageLoader object.
var jakob_mc:MovieClip = this.createEmptyMovieClip("jakob_mc", this.getNextHighestDepth());
var jakob:ImageLoader = new ImageLoader("http://www.helpexamples.com/flash/images/image1.jpg", jakob_mc, {_x:10, _y:10, _alpha:70, _rotation:-5});