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.
SyntaxInterfaces
[dynamic] class ClassName [extends ParentClass]Usage
[implements InterfaceName1 [, InterfaceNameX]*] {
// Properties
[public | private] [static] var propName:Type [= value];
// Constructor
[public | private] function ClassName(...) { ... }
// Methods
[public | private] [static] function methodName(...):Type { ... }
}
- 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.
Examples
- Use of the "this" keyword within a class is not required, but it can help improve the readability of the code.
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"
SyntaxAccess levels
interface InterfaceName [extends ParentInterface] {Usage
// Method declarations
[public] function functName(...):Type;
}
- Interfaces only contain public method declarations.
- Public (default)
- Private : Only accessible within the class and within subclasses.
Syntax
function get propertyName():Type {Usage
...
}
function set propertyName(paramName:Type):Void {
...
}
Examples
- 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.
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