Writing custom class files 235
Writing custom class files
The following example examines the parts of a class file. You learn how to write a class, and
how you can modify the class to extend the ways that you can use it with Flash. You learn
about the parts of a class and how to import them as well as related information about
working with custom class files in Flash.
You begin by looking at a very simple class. The following example shows the organization of
a simple class called UserClass.
To define a class, you use the
class keyword in an external script file (that is, not in a script
you are writing in the Actions panel). The class structure is also pertinent for interface files.
This structure is illustrated below, and following this illustration you create a class.
■ The class file begins with documentation comments that include a general description of
the code as well as author and version information.
■ Add your import statements (if applicable).
■ Write a package statement, class declaration, or interface declaration, as follows:
class UserClass {...}
■ Include any necessary class or interface implementation comments. In these comments,
add information that is pertinent for the entire class or interface.
■ Add all your static variables. Write the public class variables first and follow them with
private class variables.
■ Add instance variables. Write the public member variables first, and follow them with
private member variables.
■ Add the constructor statement, such as the one in the following example:
public function UserClass(username:String, password:String) {...}
■ Write your methods. Group methods by their functionality, not by their accessibility or
scope. Organizing methods this way helps improve the readability and clarity of
your code.
■ Write the getter/setter methods into the class file.
The following example looks at a simple ActionScript class named User.