Using Getters and Setters






Using Getters and Setters

As you're well aware by now, when you create an instance of a class you're actually creating an object. Often these objects have properties, as defined by their class files. For example, look at this class definition:

    class State {
      var population:Number;
      function State() {
        //Constructor
      }
    }

This defines the State class. This class has a single property named population, which is used to hold the number of people in the state.

Creating an instance of this class would look like this:

    var northCarolina:State = new State();

You can now set the value of the population property of the northCarolina instance in the following manner:

    northCarolina.population = 8000000;

Although this might seem fine, problems can arise if you need to change the way in which the value of population is determined. Right now, population represents a number, so setting a new numeric value involves nothing more than entering the new number. But what if the instances of the State class need to be updated so that when setting the value of population, a growth percentage of five percent is factored in automatically? You could edit every script in every project that references this property to read similar to this:

    northCarolina.population = 8000000 + (8000000 * .05);

But all that editing would take a lot of work. It's better to make your classes versatile enough to handle this kind of change simply by updating the class file. This is where getters and setters become useful. Look at the updated State class definition:

     class State {
       var population:Number;
       function State() {
         //Constructor
       }
       function setPopulation(population:Number) {
         this.population = population + (population * .05);
       }
       function getPopulation():Number {
         return population;
       }
     }

As defined by the class now, setting and getting the population property are handled by methods, which can be used in the following way:

   northCarolina.setPopulation(8000000); //automatically adds a 5% growth ¬
     rate when set
   northCarolina.getPopulation(); // returns a value of 8400000

Either of these getter or setter methods could be changed or enhanced as needed from within the class definition. As a result, all scripts in all projects that use the State class would automatically reflect the new functionality.

Because of the versatility of getters and setters, getting and setting property values directly is considered bad coding practice. Set up and use getter and setter methods instead.

Using Implicit get and set Methods

Now that you understand the power and efficiency of using getter and setter methods, it's time to introduce alternate syntax available in ActionScript 2.0. Look at the following updated State class definition:

   class State {
     var statePopulation:Number;
     function State() {
       //Constructor
     }
     function set population(statePopulation:Number) {
       this.statePopulation = statePopulation + (statePopulation * .05);
     }
     function get population():Number {
       return statePopulation;
     }
    }

Although it might not be obvious, the names of the setPopulation() and getPopulation() methods have been changed to set population and get population, respectively. This change converts the methods to what are known as implicit get and set methods. What does this mean? You get the best of both worldsproperty values can be set or retrieved within the class file by using functions, but referencing them in a script is as easy as this:

    northCarolina.population = 8000000;

Or this:

    var myVariable:Number = northCarolina.population;

With this syntax, it seems as if we're once again referencing the population property directly, but we're actually calling either the set population or get population method (depending on the task) to take care of the state's population. Notice that we changed the name of the population property to statePopulation. If we hadn't done this, using the following syntax would result in an error:

    northCarolina.population = 8000000;

Flash wouldn't know whether we were attempting to set the property named population or invoking the set population set method because doing either requires the same syntax. Changing the population property name to statePopulation solves this problem.

Note

Using implicit get and set methods offers no technical advantages over using the getter and setter methods described in the previous section, other than saving a few keystrokes.




 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows