Sunday, December 2, 2012

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 Adobe 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);    // output: static variable
trace(myST.message);          // output: instance variable
Programming Actionscript 3. Powered by Blogger.