User Guide

256 Chapter 10: Creating Custom Classes with ActionScript 2.0
The this keyword is not required in ActionScript 2.0 class definitions because the compiler
resolves the reference and adds it into the bytecode. However, using
this can improve your code’s
readability. See “Using the this keyword” on page 96.
Creating properties and methods
A classs members consist of properties (variable declarations) and methods (function definitions).
You must declare and define all properties and methods inside the class body (the curly braces
[{}]); otherwise, an error will occur during compilation.
Any variable declared within a class, but outside a function, is a property of the class. In the
following example, the Person class discussed in “Using classes: a simple example” on page 250
has two properties,
age and name, of type Number and String, respectively:
class Person {
var age:Number;
var name:String;
}
Similarly, any function declared within a class is considered a method of the class. In the Person
class example, you created a single method called
getInfo():
class Person {
var age:Number;
var name:String;
function getInfo():String {
// getInfo() method definition
}
}
The this keyword is not required in ActionScript 2.0 class definitions because the compiler
resolves the reference and adds it into the bytecode. However, using
this can improve your code’s
readability. See “Using the this keyword” on page 96.
Controlling member access
By default, any property or method of a class can be accessed by any other class: all members of a
class are public by default. However, in some cases you might want to protect data or methods of
a class from access by other classes. You need to make those members private (available only to the
class that declares or defines them).
You specify public or private members using the
public or private member attribute. For
example, the following code declares a private variable (a property) and a private method
(a function). The following class (LoginClass) defines a private property named
userName and a
private method named
getUserName().
class LoginClass {
private var userName:String;
private function getUserName():String {
return this.userName;
}
// Constructor:
function LoginClass(user:String) {
this.userName = user;