Contents / Previous / Next


New OOP Language Elements in ActionScript


class

[dynamic] class className  [ extends superClass ] 

                  [ implements interfaceName [, interfaceName... ] ]
{
   // class definition 
}
Example:
class B extends class A
{
 function B() //  constructor
  { 
    // super(); // Not necessary, inserted if omitted
  }
  
  function m():Number { return 25; }
  
  function o(s:String):Void { trace(s); }
} 
Scripts must be stored externally with a single class defined in each file, which must have the same name as the class.


import

import className

import packageName.*
className : The fully qualified name of a class you have defined in an external class file.

packageName : A directory in which you have stored related class files. As in Java import lets you access classes without specifying their fully qualified names.

Flash searches for class and interface definitions in the directories listed in classpath.


interface

myClass implements interface01 [, interface02, ...]
Example:
// filename Ia.as
interface Ia
{
  function k():Number;         // method declaration only
  function n(x:Number):Number; // without implementation 
}


// filename B.as
class B implements Ia
{
  function k():Number { return 25; }
  function n(x:Number):Number { return x+5; }
} 


public / private / static

class someClassName
{
   public var name;
   public function name() { }

   private var name;
   private function name() { }

   static var name;
   static function name() { }
}
static class members are assigned to the class itself, not to any instance of the class.
Thus a static variable or function is created only once per class.
To invoke a class method or access a class property, you reference the class name itself:
  var square_root:Number = Math.sqrt(4);


dynamic

dynamic class className  [ extends superClass ] 
                    [ implements interfaceName [, interfaceName... ] ]
{
   // class definition
}
dynamic specifies that objects based on the specified class can add and access dynamic properties at runtime.

This behavior is common in AS, for example, calling MovieClip.createTextField() adds properties to a movie clip object dynamically.

Example

class Person 
{
   var name:String;
   var age:Number;
}

var a_person:Person = new Person();
a_person.hairColor = "blue"; // compiler error
Using dynamic lets you add and access properties and methods that are not defined in the original class:
dynamic class Person 
{
   var name:String;
   var age:Number;
}

var a_person:Person = new Person();
a_person.hairColor = "blue"; // works for dynamic class
Subclasses of dynamic classes are also dynamic.


set / get

Object-oriented programming practice discourages direct access to properties within a class.

Classes typically define "get" methods that provide read access and "set" methods that provide write access to a given property.

Implicit get/set methods are a syntactic shorthand for these methods.