248 Classes
Public methods and properties
The
public keyword specifies that a variable or function is available to any caller. Because
variables and functions are public by default, the
this keyword is used primarily for stylistic
and readability benefits, indicating that the variable exists in the current scope. For example,
you might want to use the
this keyword for consistency in a block of code that also contains
private or static variables. The
this keyword can be used with either the public or
private keyword.
The following Sample class already has a public method named
myMethod():
class Sample {
private var ID:Number;
public function myMethod():Void {
this.ID = 15;
trace(this.ID); // 15
trace("myMethod");
}
}
If you want to add a public property, you use the word “public” instead of “private,” as you
can see in the following sample code:
class Sample {
private var ID:Number;
public var email:String;
public function myMethod():Void {
trace("myMethod");
}
}
Because the email property is public, you can change it within the Sample class, or directly
within a FLA.
Private methods and properties
The
private keyword specifies that a variable or function is available only to the class that
declares or defines it or to subclasses of that class. By default, a variable or function is public,
and available to any caller. Use the
this keyword if you want to restrict access to a variable or
function, as you can see in the following example:
class Sample {
private var ID:Number;
public function myMethod():Void {
this.ID = 15;
trace(this.ID); // 15
trace("myMethod");
}
}