Flash CS3 Documentation |
|||
| ActionScript 2.0 Language Reference > ActionScript language elements > Statements > private statement | |||
class someClassName{
private var name;
private function name() {
// your statements here
}
}
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 available to any caller. Use this keyword if you want to restrict access to a variable or function. This keyword is intended as a software development aid to facilitate good coding practices such as encapsulation, and not as a security mechanism to obfuscate or secure sensitive data. It does not necessarily prevent access to a variable at runtime.
You can use this keyword only in class definitions, not in interface definitions.
Availability: ActionScript 2.0; Flash Player 6
name:String - The name of the variable or function that you want to specify as private.
The following example demonstrates how to restrict access to variables or functions by using the private keyword. Create a new AS file called Alpha.as:
class Alpha {
private var privateProperty = "visible only within class and subclasses";
public var publicProperty = "visible everywhere";
}
In the same directory as Alpha.as, create a new AS file named Beta.as that contains the following code:
class Beta extends Alpha {
function Beta() {
trace("privateProperty is " + privateProperty);
}
}
As the following code demonstrates, the constructor for the Beta class is able to access the privateProperty property that is inherited from the Alpha class:
var myBeta:Beta = new Beta(); // Output: privateProperty is visible only within class and subclasses
Attempts to access the privateProperty variable from outside the Alpha class or a class that inherits from the Alpha class result in an error. The following code, which resides outside of any class, causes an error:
trace(myBeta.privateProperty); // Error
Flash CS3