Private Attribute for Methods

 < Day Day Up > 

Private Attribute for Methods

A new feature within ActionScript 2.0 gives us the ability to determine from where methods can be accessed. This is done with the keywords public and private . Methods that will not be used outside the class should be declared as private . In the following example, the methods doDrag and doDrop will be used only within the Drag class and therefore should be referenced as private :

 class Drag extends MovieClip { function Drag () { onPress = doDrag; onRelease = doDrop; } private function doDrag () :Void {       this.startDrag(); } private function doDrop () :Void {       this.stopDrag(); } } } 

N OTE

The extends keyword means that we can use all the methods of the MovieClip class.


In Flash MX 2004, private methods are not truly private because they are inherited by any subclasses from that class. In the following example, the RollEffect class inherits all methods from the Drag class even though the methods have been assigned the private attribute in the Drag class:

 class RollEffect extends Drag {       function RollEffect ()       {             onPress = doDrag;             onRelease = doDrop;             onRollOver = doAlpha;       } private function doAlpha () :Void {       this._alpha = 50; } } 

In most OOP languages, inheriting from a method, but not being able to access it, is known as working with a protected method. However, in ActionScript 2.0, protected methods are referred to as private. There is no way in ActionScript 2.0 to build a class that does not inherit its private properties.

Of course, it is certainly possible in ActionScript 2.0 to access a property directly, as shown in the following code:

 class Loan { var interestRate function Loan (latestRate :Number){       this.interestRate = latestRate       } } 

There is nothing wrong with this approach, but using methods will result in more maintainable and scaleable applications.

Data Hiding

As we saw in Chapter 3, it is a best practice to always access properties within a class through methods instead of directly accessing the property. When we use methods to access a property, we have more control over any changes that are made because the property itself is hidden and accessed only through external methods. Data hiding makes code more maintainable and scalable.

Let's take the TextField class in Flash MX 2004 as an example of data hiding. Throughout many applications, people have repeatedly accessed the .text property directly, as shown in Figure 4.4.

Figure 4.4. Setting the .text property directly.

graphics/04fig04.gif

Setting a property directly could result in maintainability problems if Macromedia ever decided to change the name of the .text property to another name. We would have to go through every single line of code of every application built and change the property references to the new property name. However, if the text field had been built with getter/setter methods (discussed in the next section), all we would have to do is change the property name within the method and it would not affect the end application at all.

N OTE

Of course, this is just an example. One of the top priorities of any Flash release is backward compatibility, so it is highly unlikely that the .text property will ever change; however, we are not always so lucky with custom classes because property names can change on a whim based on the customer.


Getters and Setters

To completely understand the importance of getter/setter methods, we are going to create a movie clip with a text field inside it. Instead of accessing the .text property directly, we are going to access the property through two new methods, getText() and setText() . The first step is to define a new class that extends the current text field class, as shown in the following code:

 class TextControl extends TextField {       function TextControl () {}; //Constructor } 

N OTE

The extends keyword is covered in detail in Chapter 5. Used in the preceding code, it simply means that we can use all the methods of the TextField class.


In Chapter 3, we looked at two ways of creating getter/setter methods. We determined that the best practice was not to use the keywords get and set . Because we extended the textField class, the .text property is inherited by our new TextControl class. All we need to do is build getter/setter methods for that class.

For instance, in the setText method, we set the autoSize property of the textField to true , and we populate the text property to the value that is passed in from the method. The getText method returns the value of whatever is in the current textField . The advantage of this approach is that it separates the development of the class from the use of the class. No matter what happens, the TextControl class will use getText() and setText() even if other parts of the code change.

As an example of the functionality, assume that our client would now like all textFields to be one font. We could set that up in setText():

 class TextControl extends TextField {       function TextControl () {};       public function setText(textToSet :String) :Void       {             trace (this.theText);             this.theText.autoSize = true;             this.theText.text = textToSet;       }       public function getText () :String       {             return this.theText.text;       } } 

The final step in building a new TextControl class is to link a visual object, which, in this case, is a text field inside a movie clip, to the class we just built. You can do this with the following step sequence.

  1. Save the class as TextControl.as.

  2. Create a new FLA file and create a new movie clip by choosing Insert > New Symbol.

  3. Name the symbol TextControlSymbol .

  4. Click Advanced and in the class field, type TextControl .

  5. Place a TextField object inside the movie clip.

  6. Assign the text field an instance name of theText .

  7. Drag an instance of the TextControlSymbol onto the Stage.

  8. Assign the TextControlSymbol instance a name of myText .

  9. Add the following code to populate the textField with a method:

     myText.setText("OOP ActionScript"); 

Building Implicit Getters/Setters

Implicit getter/setter methods are one way to access properties in ActionScript 2.0. Implicit methods use the keywords get and set . The big difference between explicit and implicit getter/setter methods is how they are called. They are referenced by the end user as properties even though behind the scenes they are actually methods. For example, in v1 components , setDataProvider() was a method. In v2 components, it is now an implicit setter property (still really a method) and accessed as shown in the following code:

 var myArray :Array = new Array(); myArray[0] ="US"; myArray[1] = "Canada"; var myCombo.dataProvider = myArray; 

These getter/setter methods cannot share the same name as the underlying property. The following code would be a bad practice because text is an existing property of the TextField class:

 class TextControl extends TextField {       function OOText () {};       public function set text(textToSet :String) :Void       {             trace (this.theText);             this.theText.autoSize = true;             this.theText.text = textToSet;       }       public function get text () :String       {             return this.theText.text;       } } 

If we wanted to use implicit getter/setters, we would need to change the name of the methods to something else, such as _text , as shown in the following code:

 class TextControl extends TextField {       function TextControl () {};       public function set _text(textToSet :String) :Void       {             trace (this.theText);             this.theText.autoSize = true;             this.theText.text = textToSet;       }       public function get _text () :String       {             return this.theText.text;       } } 

The getter/setter methods would be invoked by simply referencing the _text method just like a property, as shown in the following code:

 <instance>._text = "OOPActionScript"; <variable> = <instance>._text; 

With implicit getters/setters, it is impossible for the end user to tell if a method or a property is being accessed and this is why we encourage the use of explicit getters/setters. Of course, by using implicit getter/setters, we still only have to make changes in the class, and not in the use of the class, so those advantages are retained.

 < Day Day Up > 


Object-Oriented Programming with ActionScript 2.0
Object-Oriented Programming with ActionScript 2.0
ISBN: 0735713804
EAN: 2147483647
Year: 2004
Pages: 162

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