Flash CS4 Professional ActionScript 2.0

Using getter and setter methods

The syntax for getter and setter methods is as follows:

  • A getter method does not take any parameters and always returns a value.
  • A setter method always takes a parameter and never returns a value.

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:

  1. Select File > New and then select ActionScript File, and then click OK.
  2. Type the following code into the Script window:
    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;
        }
    }
    
    
  3. Save the ActionScript document as Login.as.

    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.

  4. Select File > New and then select Flash Document to create a new FLA, and save it as login_test.fla in the same directory as Login.as.
  5. Add the following ActionScript to Frame 1 of the main Timeline:
    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
    
    
  6. Select Control > Test Movie to test the file.

    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:

  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 this case you could not also have a property named userName in the same class.

  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.

  6. Save the Flash document and select Control > Test Movie to test the file.

    Flash displays the following information in the Output panel:

    RickyM
    EnriqueI
    
    

NOTE

 

You cannot use getter and setter method attributes in interface method declarations.