Class definitions

ActionScript 3.0 class definitions use syntax that is similar to that used in ActionScript 2.0 class definitions. Proper syntax for a class definition calls for the class keyword followed by the class name. The class body, which is enclosed by curly braces({}), follows the class name. For example, the following code creates a class named Shape that contains one variable, named visible:

public class Shape
{
    var visible:Boolean = true;
}

One significant syntax change involves class definitions that are inside a package. In ActionScript 2.0, if a class is inside a package, the package name must be included in the class declaration. In ActionScript 3.0, which introduces the package statement, the package name must be included in the package declaration instead of in the class declaration. For example, the following class declarations show how the BitmapData class, which is part of the flash.display package, is defined in ActionScript 2.0 and ActionScript 3.0:

// ActionScript 2.0
class flash.display.BitmapData {}

// ActionScript 3.0
package flash.display
{
    public class BitmapData {}
}

Subtopics

Class attributes
Class body

Class attributes

ActionScript 3.0 allows you to modify class definitions using one of the following four attributes:

Attribute

Definition

dynamic

Allow properties to be added to instances at run time.

final

Must not be extended by another class.

internal (default)

Visible to references inside the current package.

public

Visible to references everywhere.

For each of these attributes, except for internal, you must explicitly include the attribute to get the associated behavior. For example, if you do not include the dynamic attribute when defining a class, you will not be able to add properties to a class instance at run time. You explicitly assign an attribute by placing it at the beginning of the class definition, as the following code demonstrates:

dynamic class Shape {}

Notice that the list does not include an attribute named abstract. This is because abstract classes are not supported in ActionScript 3.0. Notice also that the list does not include attributes named private and protected. These attributes have meaning only inside a class definition, and cannot be applied to classes themselves. If you do not want a class to be publicly visible outside a package, place the class inside a package and mark the class with the internal attribute. Alternatively, you can omit both the internal and public attributes, and the compiler will automatically add the internal attribute for you. If you do not want a class to be visible outside the source file in which it is defined, place the class at the bottom of your source file below the closing curly brace of the package definition.

Class body

The class body, which is enclosed by curly braces, is used to define the variables, constants, and methods of your class. The following example shows the declaration for the Accessibility class in the Flash Player API:

public final class Accessibility
{
    public static function get active():Boolean;
    public static function updateProperties():void;
}

You can also define a namespace inside a class body. The following example shows how a namespace can be defined within a class body and used as an attribute of a method in that class:

public class SampleClass
{
    public namespace sampleNamespace;
    sampleNamespace function doSomething():void;
}

ActionScript 3.0 allows you to include not only definitions in a class body, but also statements. Statements that are inside a class body, but outside a method definition, are executed exactly once--when the class definition is first encountered and the associated class object is created. The following example includes a call to an external function, hello(), and a trace statement that outputs a confirmation message when the class is defined:

function hello():String
{
    trace ("hola");
}
class SampleClass
{
    hello();
    trace("class created");
}
// output when class is created
hola
class created

In contrast to previous versions of ActionScript, in ActionScript 3.0 it is permissible to define a static property and an instance property with the same name in the same class body. For example, the following code declares a static variable named message and an instance variable of the same name:

class StaticTest
{
    static var message:String = "static variable";
    var message:String = "instance variable";
}
// In your script
var myST:StaticTest = new StaticTest();
trace(StaticTest.message); // static variable
trace(myST.message);       // instance variable

Flex 2.01

Take a survey


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flex/201/html/04_OO_Programming_161_03.html