Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Classes > About working with custom classes in an application > About dynamic classes > 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, userName and age.
To create a class that is not dynamic:
class Person {
public var userName:String;
public 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.
firstPerson), and try to assign a value to a property called hairColor (which doesn't exist in the Person class):
var firstPerson:Person = new Person(); firstPerson.hairColor = "blue"; // Error. There is no property with the name 'hairColor'.
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 might 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.
To create a dynamic class:
dynamic class Person2 {
public var userName:String;
public var age:Number;
}
This ActionScript adds the dynamic keyword to the Person class in the previous example. Instances of the Person2 class can add and access properties and methods that are not defined in this class.
firstPerson), and assign a value to a property called hairColor (which doesn't exist in the Person2 class).
var firstPerson:Person2 = new Person2(); firstPerson.hairColor = "blue"; trace(firstPerson.hairColor); // blue
Because the custom Flash class is dynamic, you can add methods and properties to the class at runtime (when the SWF file plays). When you test the code the text blue should be displayed in the Output panel.
When you develop applications, you wouldn't want to make classes dynamic unless you needed to. One reason not to use dynamic classes is that type checking on dynamic classes is less strict than type checking on nondynamic classes, because members accessed inside the class definition and on class instances are not compared with those defined in the class scope. Class member functions, however, can still be type checked for return types and parameter types.
Subclasses of dynamic classes are also dynamic, with one exception. Subclasses of the MovieClip class are not dynamic by default, even though the MovieClip class itself is dynamic. This implementation provides you with more control over subclasses of the MovieClip class, because you can choose to make your subclasses dynamic or not:
class A extends MovieClip {} // A is not dynamic
dynamic class B extends A {} // B is dynamic
class C extends B {} // C is dynamic
class D extends A {} // D is not dynamic
dynamic class E extends MovieClip{} // E is dynamic
For information on subclasses, see Inheritance.
|
|
|
|