ActionScript 3.0 Cookbook: Chapter 2, Custom Classes
Pages: 1, 2, 3, 4

Method names are subject to the same rules as variables and properties. That means that method names must contain only numbers, letters, underscores, and dollar signs. Additionally, while method names can contain numbers, they cannot begin with numbers. By convention, method names start with lowercase characters. There is one exception to that guideline, however. Every class can have a special method with the same name as the class itself. This special method is called the constructor, and as the name implies, you can use the function to construct new instances of the class. In ActionScript 3.0, all constructors must be public (this is a change from ActionScript 2.0). Unlike standard methods, constructors cannot return values, and they must not declare a return type. The following declares a constructor method for the Example class:

package {
    public class Example {
        private var _id:String;
        public function Example( ) {
            _id = "Example Class";
        }
        public function getId( ):String {
            return _id;
        }
    }
}

The following illustrates how to construct an instance of the Example class:

var example:Example = new Example( );
trace(example.getId( )); // Displays: Example Class

See Also

Recipe 2.2

Section 2.2: Determining Where to Save a Class

Problem

You want to determine where to save a class file.

Solution

Save the file in a directory path corresponding to the package. Then, if necessary, add the top-level directory to the classpath.

Discussion

Class files must always be saved in a directory path that corresponds to the class package. For example, com.examplecorp.net.messaging.email.MessageManager must be saved in com/examplecorp/net/messaging/email/MessageManager.as. The compiler knows to look for classes where the path corresponds to the package. However, the compiler also must know where to look for the top-level directory containing the subdirectories. In the example, the compiler needs to know where the com directory is on the system. The compiler knows where to look because of something called the classpath. The default classpath for any Flex or Flash project includes the project directory. For example, if the com directory is saved in the same directory as the .fla file (Flash) or the main MXML or ActionScript file (Flex), then the compiler will find the classes. However, you may want to save files in a different directory. For example, if you have a common library used by many projects, you may want to save that library in one location rather than making copies for each project. You can add to and edit the classpath so the compiler knows where to look for all your custom classes.

For Flash, you can edit the classpath either at the project level or globally. At the project level, select File → Publish Settings, and select the ActionScript Settings button in the Flash tab. For the global classpath, select Edit → Preferences, and click the classpath. You can click the + button to add a new directory to the classpath. For example if com directory for the common library is stored in C:\libraries, then you would add C:\libraries to the classpath.

For Flex, you can only set the classpath for a project. Using Flex Builder, right-click a project in the project navigator, select Properties, and then select Flex/ActionScript Build Path from the left menu. In the Source Path tab, you can add to and edit the classpath. If you're using the SDK rather than Flex Builder, you have to set the classpath when you're compiling your project. Using mxmlc (the command-line compiler included in the Flex SDK), you can add a -source-path option, followed by a list of classpath directories, as shown here:

    mxmlc -source-path . C:\libraries ExampleApplication.as

See Also

Recipe 2.1

Section 2.3: Creating Properties That Behave As Methods

Problem

You want to use public properties that behave like methods so you don't break encapsulation.

Solution

Use implicit getters and setters.

Discussion

As mentioned in Recipe 2.1, all properties should be declared as private or protected. public properties are not a good idea because of a principal called encapsulation. Good encapsulation is something to strive for. It means that a class doesn't expose its internals in a way that it can be easily broken; public properties can enable developers to easily break a class or an instance of a class. Consider the following simple example that uses a public property:

package {
    public class Counter {
        public var count:uint;
        public function Counter( ) {
            count = 0;
        }
    }
}

You can then construct an instance of Counter, and you can change the count property value, as shown here:

var counter:Counter = new Counter( );
counter.count++;

However, what if the business rules of the application state that a Counter should never exceed 100? You can see that the Counter class with a public count property makes it quite easy to break that rule.

One option is to use explicit getters and setters, as in the following example:

package {
    public class Counter {
        private var _count:uint;
        public function Counter( ) {
            _count = 0;
        }
        public function getCount( ):uint {
            return _count;
        }
        public function setCount(value:uint):void {
            if(value < 100) {
                 _count = value;
            }
            else {
                throw Error( );
            }
        }
    }
}

This excerpt is from ActionScript 3.0 Cookbook. Well before Ajax and Windows Presentation Foundation, Macromedia Flash provided the first method for building "rich" web pages. Now, Adobe is making Flash a full-fledged development environment, and learning ActionScript 3.0 is key. That's a challenge for even the most experienced Flash developer. This Cookbook offers more than 300 solutions to solve a wide range of coding dilemmas, so you can learn to work with the new version right away.

buy button

Another option is to use implicit getters and setters. Implicit getters and setters are declared as methods, but they look like properties. The syntax for a getter is as follows:

public function get name( ):Datatype {
    
}

The syntax for a setter is as follows:

public function set name(value:Datatype):void {

}

The following defines count with implicit getter and setter methods:

package {
    public class Counter {
        private var _count:uint;
        public function Counter( ) {
            _count = 0;
        }
        public function get count( ):uint {
            return _count;
        }
        public function set count(value:uint):void {
            if(value < 100) {
                _count = value;
            }
            else {
                throw Error( );
            }
        }
    }
}

You can then treat count as though it were a public property:

counter.count = 5;
trace(counter.count);

See Also

Recipe 2.1

Section 2.4: Creating Static Methods and Properties

Problem

You want to create methods and properties that are directly accessible from the class rather than from instances of the class.

Solution

Use the static attribute when declaring the property or method.

Discussion

By default, properties and methods are instance properties and methods, which means they are defined for each instance of the class. If the Example class defines a _id property and a getId( ) method then, by default, each instance of Example has its own _id property and getId( ) method. However, there are cases in which you want the property or method to be associated with the class itself rather than with instances of the class. That means that no matter how many instances of the class there may be, there is just one property or method. Such properties and methods are called static properties and methods.

There are examples of static properties and methods in several of the intrinsic Flash Player classes. For example, the Math class defines a round( ) method. The round( ) method is static and is, therefore, accessible directly from the class:

trace(Math.round(1.2345));

The Math class consists entirely of static methods and constants. However, a class can have both static and instance methods and/or properties. For example, the String class consists primarily of instance properties and methods. However, the fromCharCode( ) method is declared as static. The fromCharCode( ) method returns a string based on the character codes passed to the method. Since the method isn't associated with any one String instance, it does not make sense to make the method an instance method. However, it does make sense to declare the method as a static method.

You can declare a property or method as static using the static attribute. The static attribute is always used in combination with the public, private, protected, or internal attribute.

For example, the following declares a private static property called _example:

static private var _example:String;

The order in which the attributes appear doesn't matter. For example, static private is the equivalent to private static.

Pages: 1, 2, 3, 4

Next Pagearrow