User Guide
Creating dynamic classes 259
If you don’t place a call to super() in the constructor function of a subclass, the compiler
automatically generates a call to the constructor of its immediate superclass with no parameters as
the first statement of the function. If the superclass doesn’t have a constructor, the compiler
creates an empty constructor function and then generates a call to it from the subclass. However,
if the superclass takes parameters in its definition, you must create a constructor in the subclass
and call the superclass with the required parameters.
Multiple inheritance, or inheriting from more than one class, is not allowed in ActionScript 2.0.
However, classes can effectively inherit from multiple classes if you use individual
extends
statements, as shown in the following example:
// not allowed
class C extends A, B {}
// allowed
class B extends A {}
class C extends B {}
You can also use interfaces to provide a limited form of multiple inheritance. See “Interfaces”
on page 249 and “Creating and using interfaces” on page 261.
Creating dynamic classes
By default, the properties and methods of a class are fixed. That is, an instance of a class can’t
create or access properties or methods that weren’t originally declared or defined by the class. For
example, consider a Person class that defines two properties,
name and age:
class Person {
var name:String;
var age:Number;
}
If, in another script, you create an instance of the Person class and try to access a property of the
class that doesn’t exist, the compiler generates an error. For example, the following code creates a
new instance of the Person class (
a_person) and then tries to assign a value to a property named
hairColor, which doesn’t exist:
var a_person:Person = new Person();
a_person.hairColor = "blue"; // compiler error
This code causes a compiler error because the Person class doesn’t declare a property named
hairColor. In most cases, this is exactly what you want to happen. Compiler errors may not seem
desirable, but they are very beneficial to programmers; good error messages help you to write
correct code, by pointing out mistakes early in the coding process.
In some cases, however, you might want to add and access properties or methods of a class at
runtime that aren’t defined in the original class definition. The
dynamic class modifier lets you do
just that. For example, the following code adds the
dynamic modifier to the Person class
discussed previously:
dynamic class Person2 {
var name:String;
var age:Number;
}