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. Next, we'll discuss what this means and how you can use this functionality when creating class members.

Public and Private Members

By default, all members of a class (its properties and methods) are public. This means that the property or methods of that class can be accessed by instances of that class. Look again at our State class definition:

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


The members in this class, statePopulation, setPopulation(), and getPopulation(), are all publicly accessible by instances of the class. For example, the following script shows us setting the value of statePopulation directly:

    northCarolina.statePopulation = 8000000;


As mentioned in the preceding section, although this property is used to hold a numeric value representing the state's population, it's better to use getter and setter methods to get and set the value, which our class definition has been set up to do. In other words, statePopulation should be a variable that cannot be directly accessed from outside the class definition. To make this change, you use the private and public keywords to indicate each member's access in a class:

    class State {       private var statePopulation:Number;       public function State() {          //Constructor       }       public function setPopulation(num:Number) {         statePopulation = num + (num * .05);       }       public function getPopulation():Number {         return statePopulation;       }     }


The statePopulation property has been declared as a private member of the class. As a result, only code within the class definition itself, such as the setPopulation() and getPopulation() methods, can access it directly. Attempting to access it from an instance in the following way results in an error:

    northCarolina.statePopulation = 8000000;


Any property or method can be declared private or public, as you see fit.

Why would you want to hide some members in this way? Because a robust class definition often has many properties and methods that are used to take care of tasks internally; they have functionality built into them that should not be exposed for use outside the class. Let's look at a real-world example.

When most people use a computer, all they want to know is how to turn it on and off, and how to interact with it via the keyboard and mouse. Most people aren't interested in knowing how it works internally (information about how the hardware is sending data through the circuits or the way the hard drive is reading and writing datathe internal workings of the computer that are important, but that don't affect how it's used). In the same sense, it's not necessary to open all the functionalities of a class to direct access from an instance of that class. A class might have 10 properties and 15 methods internally that affect how instances of that class work, but maybe only 3 or 4 methods that should be directly referenced from an instance.

In the long run, setting member access helps prevent bugs because, as mentioned earlier, it prevents you from scripting an object in a way that you shouldn't. If you attempt to use an object in the wrong way (attempting to use or access a private class member that should only be used internally within the class), Flash displays an error and lets you know that you need to reexamine your code.

Static Members

By default, every member of a class is duplicated within an instance whenever that instance is created. Consider the State class example that we used a few times in this lesson. For every new instance of that class, a copy of the statePopulation property is created within the instance. This makes sense because every state has its own population. In other words, although every instance of the State class has a statePopulation property, the value of that property might differ for each instance.

There might be times, however, when you need a property that is not only accessible by every instance of a class, but has a universal value across all instances. This is the functionality that static properties provide.

If a property is static, it's created in memory only once. All instances of the class see the same copy of this member. If any instance of the class edits the value of the property, all instances of the class see the new value.

Take the following class as an example:

   class Star {      static private var starsInTheSky:Number = 1000000000;      public function Star() {        //Constructor      }      public function setTotalStars(num:Number) {        starsInTheSky = num;      }      public function getTotalStars():Number {        return starsInTheSky;      }    }


The property starsInTheSky has been specified as static and is used to store the total number of stars that can be seen in the sky. If we were to create several instances of this class, as follows:

    var star1:Star = new Star();     var star2:Star = new Star();     var star3:Star = new Star();


referencing the starsInTheSky property from any one of these instances would result in the same value:

    star1.getTotalStars(); //has a value of 1000000000     star2.getTotalStars(); //has a value of 1000000000     star3.getTotalStars(); //has a value of 1000000000


If a star goes supernova or another star is born, the setTotalStars() method can be executed to change the value of starsInTheSky. When the value is changed to 1000000037, all Star class instances see the following new value:

    star1.getTotalStars(); //has a value of 1000000037     star2.getTotalStars(); //has a value of 1000000037     star3.getTotalStars(); //has a value of 1000000037


When would this kind of functionality be useful? Imagine having a class in which each instance has to load data from the same URL. If the URL was created as a static property of the class, it could be changed at a later time, and all the instances would automatically load from the new URL.

Methods can be static, too. Take a look at the following example:

   class Sky {      static private var starsInTheSky:Number = 1000000000;      static public function setTotalStars(num:Number) {        starsInTheSky = num;      }      static public function getTotalStars():Number {        return starsInTheSky;      }    }


This class has the starsInTheSky static property and two static methods: setTotalStars() and getTotalStars().

Similar to static properties, static methods have a universal functionality. These methods can be called from any instance to update and return the value of starsInTheSky.

An interesting aspect about static methods (and properties) is that they can be accessed simply by referencing the class name, followed by the name of the method, such as the following:

    Sky.setTotalStars(999999999); //One star died     var numStars:Number = Sky.getTotalStars();


In this example, we used the class name (Sky) instead of an instance name to invoke both the setTotalStars() and getTotalStars() methods. This makes sense due to the classwide functionality of static methods and properties. You used similar syntax when invoking methods of the Math class, which has several static methods:

    Math.round();     Math.random();     Math.floor();     Math.ceil();


Note

A static method can change the value of a static property, but a static method cannot change the value of an instance-based (nonstatic) property.





Macromedia Flash 8 ActionScript Training from the Source
Macromedia Flash 8 ActionScript: Training from the Source
ISBN: 0321336194
EAN: 2147483647
Year: 2007
Pages: 221

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net