To use implicit getter and setter methods:
1.
Select File > New and then select ActionScript File, and then click OK.
2.
Type the following code into the Script window:
class Login2 {
private var __username:String;
public function Login2(username:String) {
this.__username = username;
}
public function get userName():String {
return this.__username;
}
public function set userName(value:String):Void {
this.__username = value;
}
}
3.
Save the ActionScript document as Login2.as.
Remember that a getter method does not take any parameters. A setter method must take
exactly one required parameter. A setter method can have the same name as a getter
method in the same scope. Getter and setter methods cannot have the same names as
other properties. For example, in the previous example code you defined getter and setter
methods named
userName
in the same class.
userName
4.
Select File > New and then select Flash Document to create a new FLA, and save it as
login2_test.fla in the same directory as Login2.as.
5.
Add the following ActionScript to Frame 1 of the main Timeline:
var user:Login2 = new Login2("RickyM");
// calling "get" method
var userNameStr:String = user.userName;
trace(userNameStr); // RickyM
// calling "set" method
user.userName = "EnriqueI";
trace(user.userName); // EnriqueI
Unlike ordinary methods, you invoke getter and setter methods without any parentheses
or arguments. You invoke getter and setter methods as you would a property by the
same name.
258
Classes
; in this case you could not also have a property named
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?