class State
{
var population:Number;
function State( )
{
//constructor
}
function setPopulation( population:Number) { this.population = population + ( population * 0.5 ); }
function getPopulation( ):Number { return population; }
}
Because of the versatility of getters and setters, getting and setting property values directly is considered bad coding practice. Set up and use Getter andSetter methods instead.
Using Implict Get and Set Methods
class State {
var statePopulation:Number;
function State( ) { //constructor }
function set population( statePopulation:Number)
{
this.statePopulation = statePopulation + ( statePopulation * 0.5 );
}
function get population( ):Number
{
return statePopulation;
}
}
Though 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 = 80000000;
OR this var myVariable:Number = northCarolina.population;
With this syntax, it seems as if we’re once again referencing the population propertydirectly, but we’re actually calling either the set/ get population method 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 = 80000000;
Flash wouldn’t know whether we were attempting to set the property named population or invoking the set population method because doing either requires the same syntax. Changing the property name to statePopulation solves this problem.
Note: Using implicit get and set methods offers no technical advantages over using the getter and setter method as described earlier, other than saving a few keystrokes.
Defining Members
Not all class members are created equal. Using special keywords, members can be configured in various ways, allowing you to specify how the member is accessed and the scope of its influence. private, public.
Static Members
If a property is static, it’s created in memory only once. All instances of the classse the same copy of this member. If any instance of the class edits the value of the propert, all instances of the class see the new value.
Methods can be static too, thus providing universal functionality. These methods can be called from any instance to update and return the values of static properties.
An interesting aspect about static methods & properties is that they can be accessed simply by referencing the class name, followed by the name of the property/ method, such as the following:
Sky.setTotalStars(99999999999);
var numStars:Number = Sky.getTotalStars( );
Note: A static method can change the value of a static property, but a static method cannot change the value of an instance-based (non-static) property.
Understanding Inheritance
A class can gain (inherit) all members from another class. This is called inheritance. The class that’s gaining the members is called a subclass, and the class from which it inherits is called the superclass.
A class can only extend from 1 other class. Replacing a property value or method of the superclass with a new one in the subclass is caled overriding.
You can explicitly call the constructor of the base superclass like this
super( );
More on superclasses … to be continued