User Guide
208 ActionScript language elements
Example
The following example demonstrates how you can hide certain properties within a class using
the
private keyword. Create a new AS file called Login.as.
class Login {
private var loginUserName:String;
private var loginPassword:String;
public function Login(param_username:String, param_password:String) {
this.loginUserName = param_username;
this.loginPassword = param_password;
}
public function get username():String {
return this.loginUserName;
}
public function set username(param_username:String):Void {
this.loginUserName = param_username;
}
public function set password(param_password:String):Void {
this.loginPassword = param_password;
}
}
In the same directory as Login.as, create a new FLA or AS document. Enter the following
ActionScript in Frame 1 of the Timeline.
import Login;
var gus:Login = new Login("Gus", "Smith");
trace(gus.username); // output: Gus
trace(gus.password); // output: undefined
trace(gus.loginPassword); // error
Because loginPassword is a private variable, you cannot access it from outside the Login.as
class file. Attempts to access the private variable generate an error message.
See also
public statement
public statement
class someClassName{ public var name; public function name() { // your
statements here } }
Note: To use this keyword, you must specify ActionScript 2.0 and Flash Player 6 or later in
the Flash tab of your FLA file's Publish Settings dialog box. This keyword is supported only
when used in external script files, not in scripts written in the Actions panel.