Adding methods and properties

To create the properties for the ClassA and ClassB classes, use the var keyword to define variables.

NOTE

 

The following three exercises are part of Example: Writing custom classes. If you do not wish to progress through the example, you can download the class files from www.helpexamples.com/flash/learnas/classes/.

To add properties to the ClassA and ClassB classes:

  1. Open ClassA.as and ClassB.as in the Flash authoring tool.
  2. Modify the ClassA.as ActionScript file to match the following code (the changes to make appear in boldface):
    class com.adobe.utils.ClassA {
        static var _className:String;
        function ClassA() {
            trace("ClassA constructor");
        }
    }
    

    The previous block of code adds a single new static variable, _className, which contains the name of the current class.

  3. Modify the ClassB class and add the static variable so it is similar to the previous code.
  4. Save both ActionScript files before you proceed.

    TIP

     

    By convention, class properties are defined at the top of the class body. Defining them at the top makes the code easier to understand, but isn't required.

You use the post-colon syntax (for example, var username:String and var age:Number) in the variable declarations. This is an example of strict data typing. When you type a variable using the var variableName:variableType format, the ActionScript compiler ensures that any values assigned to that variable match the specified type. If the correct data type is not used in the FLA file importing this class, the compiler throws an error. For more information on strict data typing, see About assigning data types and strict data typing.

A class's members consist of properties (variable declarations) and methods (function definitions). You must declare and define all properties and methods inside the class body (the curly braces [{}]); otherwise, an error occurs during compilation. For information on members, see About public, private, and static methods and properties (members).

To add methods to the ClassA and ClassB classes:

  1. Open ClassA.as and ClassB.as in the Flash authoring tool.
  2. Modify the ClassA class file so it matches the following code (the changes to make appear in boldface):
    class com.adobe.utils.ClassA {
        static var _className:String;
    
        function ClassA() {
            trace("ClassA constructor");
        }
        function doSomething():Void {
            trace("ClassA - doSomething()");
        }
    }
    

    The block of code in boldface creates a new method in the class, which traces a string to the Output panel.

  3. In ClassA.as, select Tools > Check Syntax to check the syntax of your ActionScript file.

    If any errors are reported in the Output panel, compare the ActionScript in your script to the complete code written in the previous step. If you cannot fix the code errors, copy and paste the complete code into the Script window before you proceed.

  4. Check the syntax of ClassB.as as you did in ClassA.as.

    If any errors appear in the Output panel, copy and paste the complete code into the Script window before you proceed:

    class com.adobe.utils.ClassB {
        static var _className:String;
    
        function ClassB() {
            trace("ClassB constructor");
        }
        function doSomething():Void {
            trace("ClassB - doSomething()");
        }
    }
    
  5. Save both ActionScript files before you proceed.

You can initialize properties inline--that is, when you declare them--with default values, as shown in the following example:

class Person {
  var age:Number = 50;
  var username:String = "John Doe";
}

When you initialize properties inline, the expression on the right side of an assignment must be a compile-time constant. That is, the expression cannot refer to anything that is set or defined at runtime. Compile-time constants include string literals, numbers, Boolean values, null, and undefined, as well as constructor functions for the following top-level classes: Array, Boolean, Number, Object, and String.

To initialize properties inline:

  1. Open ClassA.as and ClassB.as in the Flash authoring tool.
  2. Modify the ClassA class file so the code matches the following ActionScript (the changes to make appear in boldface):
    class com.adobe.utils.ClassA {
        static var _className:String = "ClassA";
    
        function ClassA() {
            trace("ClassA constructor");
        }
        function doSomething():Void {
            trace("ClassA - doSomething()");
        }
    }
    

    The only difference between the existing class file and the previous block of code is there is now a value defined for the static _className variable, "ClassA".

  3. Modify the ClassB class file and add the inline property, changing the value to "ClassB".
  4. Save both ActionScript files before you proceed.

This rule applies only to instance variables (variables that are copied into each instance of a class), not class variables (variables that belong to the class).

NOTE

 

When you initialize arrays inline, only one array is created for all instances of the class.

To continue writing your class file, see Controlling member access in your classes.


Flash CS3