class

Availability

Flash Player 6.

Usage

[dynamic] class className  [ extends superClass ] 
              [ implements interfaceName [, interfaceName... ] ]
{
  // class definition here
}

Note: To use this keyword, you must specify ActionScript 2.0 and Flash Player 6 or later in the Flash tab of your FLA file's Publish Settings dialog box. This keyword is supported only when used in external script files, not in scripts written in the Actions panel.

Parameters

className The fully qualified name of the class.

superClass Optional; the name of the class that className extends (inherits from).

interfaceName Optional; the name of the interface whose methods className must implement.

Description

Statement; defines a custom class, which lets you instantiate objects that share methods and properties that you define. For example, if you are developing an invoice-tracking system, you could create an invoice class that defines all the methods and properties that each invoice should have. You would then use the new invoice() command to create invoice objects.

The name of the class must be the same as the name of the external file that contains the class. For example, if you name a class Student, the file that defines the class must be named Student.as.

The class name must be fully qualified within the file in which it is declared; that is, it must reflect the directory in which it is stored. For example, to create a class named RequiredClass that is stored in the myClasses/education/curriculum directory, you must declare the class in the RequiredClass.as file like this:

class myClasses.education.curriculum.RequiredClass {
}

For this reason, it's good practice to plan your directory structure before you begin creating classes. Otherwise, if you decide to move class files after you create them, you will have to modify the class declaration statements to reflect their new location.

You cannot nest class definitions; that is, you cannot define additional classes within a class definition.

To indicate that objects can add and access dynamic properties at runtime, precede the class statement with the dynamic keyword. To create classes based on interfaces, use the implements keyword. To create subclasses of a class, use the extends keyword. (A class can extend only one class, but can implement several interfaces.) You can use implements and extends in a single statement.

class C implements Interface_i, Interface_j    // OK
class C extends Class_d implements Interface_i, Interface_j    // OK
class C extends Class_d, Class_e    // not OK 

For more information, see Creating and using classes.

Example

The following example creates a class called Plant. Its constructor takes two parameters.

// Filename Plant.as
class Plant {
  // Define property names and types
  var leafType:String;
  var bloomSeason:String;
  // Following line is constructor
  // because it has the same name as the class
  function Plant (param_leafType:String, param_bloomSeason:String) {
      // Assign passed values to properties when new Plant object is created
    leafType = param_leafType;
    bloomSeason = param_bloomSeason;
  }
  // Create methods to return property values, because best practice
  // recommends against directly referencing a property of a class
  function getLeafType():String {return leafType};
  function getBloomSeason():String {return bloomSeason};
}

In an external script file or in the Actions panel, use the new operator to create a Plant object.

var pineTree:Plant = new Plant("Evergreen","N/A");
// Confirm parameters were passed correctly
trace(pineTree.getLeafType());
trace(pineTree.getBloomSeason());

See also

dynamic, extends, implements, interface, new