| Package | Top Level |
| Class | public class Class |
| Inheritance | Class Object |
new operator.
Some methods return an object of type Class, such as flash.net.getClassByAlias().
Other methods may have a parameter of type Class, such as flash.net.registerClassAlias().
For example:
class Foo {
}
class Foo{} is the class definition that creates a class object Foo. Additionally, the statement
new Foo() will create a new instance of class Foo, and the result will be of type Foo.
Use the class statement to declare your classes. Class objects are useful for advanced
techniques, such as the runtime assignment of classes to an existing instance object, as shown in the Examples
section below.
Any static properties and methods of a class live on the class's Class object. Class, itself, declares
prototype.
Generally, you will not need to declare or create variables of type Class manually. However, in the following
code, a class is assigned as public Class property, circleClass, and you can refer to this Class property
as a property of the main Library class:
package {
import flash.display.Sprite;
public class Library extends Sprite {
public var circleClass:Class = Circle;
public function Library() {
}
}
}
import flash.display.Shape;
class Circle extends Shape {
public function Circle(color:uint = 0xFFCC00, radius:Number = 10) {
graphics.beginFill(color);
graphics.drawCircle(radius, radius, radius);
}
}
Another SWF file can load the resulting Library.swf file and then instantiate objects of type Circle. The
following is one way, not the only way, to get access to a child SWF file's assets (other techniques include using
flash.utils.getDefnitionByName() or importing stub definitions of the child SWF file):
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
public class LibaryLoader extends Sprite {
public function LibaryLoader() {
var ldr:Loader = new Loader();
var urlReq:URLRequest = new URLRequest("Library.swf");
ldr.load(urlReq);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
}
private function loaded(event:Event):void {
var library:Object = event.target.content;
var circle:Shape = new library.circleClass();
addChild(circle);
}
}
}
| Object.prototype |
ClassA and ClassB. classToConstruct and one of type
Boolean chooseClassA, which is set to true in this case,
but your code could use a custom test expression to set the value of that variable.
package {
import flash.display.Sprite;
public class ClassExample extends Sprite {
public function ClassExample() {
var classToConstruct:Class;
var classInstance:Object;
classToConstruct = ClassA;
classInstance = new classToConstruct();
trace(classInstance); // [object ClassA]
classToConstruct = ClassB;
classInstance = new classToConstruct();
trace(classInstance); // [object ClassB]
}
}
}
class ClassA {
}
class ClassB {
}