Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 61

Programming actionscript 3.0
Table of Contents

Advertisement

Dynamic classes
A dynamic class defines an object that can be altered at run time by adding or changing
properties and methods. A class that is not dynamic, such as the String class, is a sealed class.
You cannot add properties or methods to a sealed class at run time.
You create dynamic classes by using the
example, the following code creates a dynamic class named
dynamic class Protean
{
private var privateGreeting:String = "hi";
public var publicGreeting:String = "hello";
function Protean()
{
trace("Protean instance created");
}
}
If you subsequently instantiate an instance of the
methods to it outside the class definition. For example, the following code creates an instance
of the
class and adds a property named
Protean
the instance:
var myProtean:Protean = new Protean();
myProtean.aString = "testing";
myProtean.aNumber = 3;
trace(myProtean.aString, myProtean.aNumber); // testing 3
Properties that you add to an instance of a dynamic class are run-time entities, so any type
checking is done at run time. You cannot add a type annotation to a property that you add in
this manner.
You can also add a method to the
the function to a property of the
statement into a method named
var myProtean:Protean = new Protean();
myProtean.aString = "testing";
myProtean.aNumber = 3;
myProtean.traceProtean = function ()
{
trace(this.aString, this.aNumber);
};
myProtean.traceProtean(); // testing 3
attribute when you declare a class. For
dynamic
Protean
aString
instance by defining a function and attaching
myProtean
instance. The following code moves the trace
myProtean
:
traceProtean()
:
Protean
class, you can add properties or
and a property named
Data types
to
aNumber
61

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents