6.6 Subclassing Built-in Classes

 <  Day Day Up  >  

Just as we can create subclasses of our own custom classes, we can also create subclasses of ActionScript's built-in classes, allowing us to implement specialized functionality based on an existing ActionScript class. For example, suppose we wanted to implement a timediff( ) method that would return the amount of time, in milliseconds , between two Date instances. We could put timediff( ) on a Date subclass named DateDeluxe , as follows :

 class DateDeluxe extends Date {   public function DateDeluxe (year:Number, month:Number, date:Number,                         hour:Number, min:Number,                         sec:Number, ms:Number) {     super(year, month, date, hour, min, sec, ms);   }   public function timediff (d:Date):Number {     return Math.abs(this.getTime( ) - d.getTime( ));   } } 

Notice that the DateDeluxe class must provide a constructor function that forwards arguments to the Date class constructor. Here's how we'd use the new DateDeluxe class:

 // Make a   DateDeluxe   instance var d1:DateDeluxe = new DateDeluxe( ); // Waste some time... for (var i:Number = 0; i < 10000; i++) {   // Dum dee dum... } // Make another   DateDeluxe   instance var d2:DateDeluxe = new DateDeluxe( ); // Show the elapsed time between   d1   and   d2   trace(d1.timediff(d2)); 

As attractive as the DateDeluxe example is, not all built-in ActionScript classes can be subclassed, due to various implementation factors. For example, the TextField class cannot be subclassed because there's no way to create a TextField subclass instance via MovieClip.createTextField( ) ; that method creates instances only of the TextField class and cannot be instructed to create an instance of a TextField subclass. (Instance of TextField cannot be created via new TextField( ) .)

Similarly, the XML class cannot be subclassed in many situations because it provides no way to specify the class to use when creating child nodes (which are instances of XMLNode ).

To circumvent the general problem of extending a nonextendable built-in class, you should use composition instead of inheritance. We'll study composition later in this chapter, but briefly , composition involves storing an instance of a class in a property and forwarding method and property access to that instance. Example 6-4 uses composition to create a TextFieldDeluxe class that stores a TextField instance in a property named tf . The TextFieldDeluxe class adds hypertext links to any text assigned to the htmlText property. An ActionScript 2.0 setter method, htmlText( ) , intercepts the property assignment before forwarding it on to the tf instance. The code shows only part of the class, giving you enough to understand how the method and property forwarding aspects work. The remainder of the class (not shown) would be implemented in the same way.

Example 6-4. Creating a subclass using composition
 class TextFieldDeluxe {   private var tf:TextField;   public function TextFieldDeluxe (target:MovieClip, name:String,                         depth:Number, x:Number, y:Number,                         w:Number, h:Number) {     target.createTextField(name, depth, x, y, w, h);     tf = target[name];   }   public static function addLinks (s:String):String {     // The real implementation of link insertion is not shown here.     // Instead, we just add some text to the string, as a proof of concept.     return s + "[LINK ADDED HERE]";   }   public function set htmlText (s:String):Void {     tf.htmlText = TextFieldDeluxe.addLinks(s);   }   public function get htmlText ( ):String {     return tf.htmlText;   }   public function addListener (listener:Object):Boolean {     return tf.addListener(listener);   }   public function getDepth ( ):Number {     return tf.getDepth( );   }   // ...Remainder of class not shown } 

Here's how we'd use the TextFieldDeluxe class:

 var tfd:TextFieldDeluxe = new TextFieldDeluxe(someClip_mc, "theField", 1,                                               20, 20, 400, 200); // Text field displays: Hello world[LINK ADDED HERE] tfd.htmlText = "Hello world";  trace(tfd.getDepth( ));        // Output panel displays: 1 

6.6.1 Built-in Classes that Store Function Libraries

Some built-in ActionScript classes are simply collections of class methods and class properties ”the Math , Key , Mouse , Selection , and Stage classes exist merely to store related functions and variables (e.g., Math.random( ) and Key.ENTER ). Rather than subclassing these classes, also known as static function libraries , you should distribute your own static function libraries separately. For example, rather than adding a factorial( ) method to a subclass of the Math class, you should create a custom class, say AdvancedMath , to hold your factorial( ) method. The AdvancedMath class need not (indeed, should not) be related to the Math class via inheritance.

 <  Day Day Up  >  


Essential ActionScript 2.0
Essential ActionScript 2.0
ISBN: 0596006527
EAN: 2147483647
Year: 2004
Pages: 177
Authors: Colin Moock

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