About class members

Most of the members (methods and properties) discussed so far in this chapter are of a type called instance members. For each instance member, there's a unique copy of that member in every instance of the class. For example, the email member variable of the Sample class has an instance member, because each person has a different e-mail address.

Another type of member is a class member. There is only one copy of a class member, and you use it for the entire class. Any variable declared within a class, but outside a function, is a property of the class. In the following example, the Person class has two properties, age and username, of type Number and String, respectively:

class Person {
    public var age:Number;
    public var username:String;
}

Similarly, any function declared within a class is considered a method of the class. In the Person class example, you can create a method called getInfo():

class Person {
    public var age:Number;
    public var username:String;
    public function getInfo():String {
        // getInfo() method definition
    }
}

In the previous code snippet the Person class's getInfo() method, as well as the age and username properties, are all public instance members. The age property would not be a good class member, because each person has a different age. Only properties and methods that are shared by all individuals of the class should be class members.

Suppose that you want every class to have a species variable that indicates the proper Latin name for the species that the class represents. For every Person object, the species is Homo sapiens. It would be wasteful to store a unique copy of the string "Homo sapiens" for every instance of the class, so this member should be a class member.

Class members are declared with the static keyword. For example, you could declare the species class member with the following code:

class Person {
  public static var species:String = "Homo sapiens";
  // ...
}

You can also declare methods of a class to be static, as shown in the following code:

public static function getSpecies():String {
    return Person.species;
}

Static methods can access only static properties, not instance properties. For example, the following code results in a compiler error because the class method getAge() references the instance variable age:

class Person {
    public var age:Number = 15;
    // ...
    public static function getAge():Number {
        return age; /* **Error**: Instance variables cannot be accessed in static functions. */
    }
}

To solve this problem, you could either make the method an instance method or make the variable a class variable.

For more information on class members (also called static properties), see Static methods and properties.

For samples that demonstrates how to create a dynamic menu with XML data and a custom class file, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. The sample calls the ActionScript XmlMenu() constructor and passes it two parameters: the path to the XML menu file and a reference to the current timeline. Download and decompress the Samples zip file and navigate to the ActionScript2.0/XML_Menu folder to access these samples:


Flash CS3


 

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

Current page: http://livedocs.adobe.com/flash/9.0/main/00000780.html