The syntax for getter and setter methods is as follows:
Classes typically define getter methods that provide read access and setter methods that provide write access to a given property. For example, imagine a class that contains a property called userName:
private var userName:String;
Instead of allowing instances of the class to directly access this property (user.userName = "Buster", for example), the class might have two methods, getUserName() and setUserName(), that would be implemented as shown in the next example.
To use getter and setter methods:
class Login {
private var __username:String;
public function Login(username:String) {
this.__username = username;
}
public function getUserName():String {
return this.__username;
}
public function setUserName(value:String):Void {
this.__username = value;
}
}
As you can see, getUserName() returns the current value of userName, and setUserName() sets the value of userName to the string parameter passed to the method.
var user:Login = new Login("RickyM");
// calling getUserName() method
var userName:String = user.getUserName();
trace(userName); // RickyM
// calling setUserName() method
user.setUserName("EnriqueI");
trace(user.getUserName()); // EnriqueI
Flash displays the following information in the Output panel:
RickyM EnriqueI
However, if you want to use a more concise syntax, you can use implicit getter and setter methods. Implicit getter and setter methods let you access class properties in a direct manner, while maintaining good OOP practice.
To define these methods, use the get and set method attributes. You create methods that get or set the value of a property, and add the keyword get or set before the method name, as shown in the next example.
|
NOTE |
|
Implicit getter and setter methods are syntactic shorthand for the Object.addProperty() method found in ActionScript 1.0. |
To use implicit getter and setter methods:
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;
}
}
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 this case you could not also have a property named userName in the same class.
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.
Flash displays the following information in the Output panel:
RickyM EnriqueI
|
NOTE |
|
You cannot use getter and setter method attributes in interface method declarations. |