OOP


Usage
  • Each class and interface definition must exist in a separate .as file.
  • The name of the .as file must match the name of the class or interface.
  • The Flash compiler must know the location of the .as files.
  • Classes and interfaces can be combined to form a package.
Classes
Syntax
[dynamic] class ClassName [extends ParentClass]
                           [implements InterfaceName1 [, InterfaceNameX]*] {
    // Properties
    [public | private] [static] var propName:Type [= value];

    // Constructor
    [public | private] function ClassName(...) { ... }

    // Methods
    [public | private] [static] function methodName(...):Type { ... }
}
Usage
  • dynamic : Additional properties and methods for objects can be added and accessed at run-time.
  • static : The property or method belongs to the class, not to individual objects; static members can be accessed using the class name, or using an object.
  • Use of the "this" keyword within a class is not required, but it can help improve the readability of the code.
Examples
class MyClass {
    // Properties
    private var info:String = "";
    private var data:Number;


    // Constructor
    public function MyClass(info:String, data:Number) {
        this.info = info;
        this.data = data;
    }


    // Methods
    public function getDetails():String {
        return (this.info + ", " + this.data);
    }
}
var myClass:MyClass = new MyClass("alpha", 1);
var details:String = myClass.getDetails();   // "alpha, 1"
Interfaces
Syntax
interface InterfaceName [extends ParentInterface] {
    // Method declarations
    [public] function functName(...):Type;
}
Usage
  • Interfaces only contain public method declarations.
Access levels
  • Public (default)
  • Private : Only accessible within the class and within subclasses.
Getters and setters
Syntax
function get propertyName():Type {
    ...
}
function set propertyName(paramName:Type):Void {
    ...
}
Usage
  • Implicit getter / setter methods
  • Combines the abstraction of explicit getter / setter methods with the intuitive syntax of directly-accessed properties
  • The name of the getter / setter methods must be different from the actual property name.
Examples
class MyClass {
    // Properties
    private var m_name:String = "";
    ...
    // Methods
    public function get name():String {
        return this.m_name;
    }
    public function set name(aName:String):Void {
        this.m_name = aName;
    }
}
var myClass:MyClass = new MyClass();
myClass.name = "Harvey";             // Set
var name:String = myClass.name;      // Get