Tải bản đầy đủ (.pdf) (5 trang)

Tài liệu Getters and Setters docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (13.24 KB, 5 trang )





< Day Day Up >

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
obviously 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;


While this may 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 5 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(num:Number){

population = num + (num * .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.
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 that might seem a 180-degree turn from what you just
learned. Look at the following updated State class definition:

class State {

var statePopulation:Number;

function State() {

//Constructor

}

function set population(num:Number){

statePopulation = num + (num * .05);

}

function get population():Number{

return statePopulation;

}

}


While 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 worlds—property 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:

northCarolina.population = 8000000;


would result in an error. Flash wouldn't know if 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.



< Day Day Up >

×