Make your own free website on Tripod.com

How to sublass the building Math Class
ericlin@ms1.hinet.net


Math is a building object. A collection of many methods. Or, in the view of AS 2, it is a Class containing many static methods.

For other building Class such as "Date","Color","Textformat", we first use "new" and constructor to create an instance of that Class and then implement its methods. That is not the case for Math class. We dont create instance of Math. We call the Class methods directly the way as "Math.pow(n,2);".


Lets try to add a custom function "squareIt(n)" to our Math Class. In Flash MX, we can easily extend the function of Math, by Math.squareIt=function(){.....}. But, in MX2004, we can't do this to a Class that is not dynamic. So, we need subclass the Math class. Lets create a subclass as Math2 class.

class Math2 extends Math {
  
    static function squareIt(n:Number) {
        return pow(n, 2);
    }
}


Lets try this first:

trace(Math2.squareIt(-5));

Hurray ! It works. It outputs '25';

Please note that, in the subclass, in the function squareIt, we can access the method "pow" as if it is a static method of the subclass. We don't need to say "Math.pow". This is what we say "inheritance" and it seems well worked.


Lets test it further:

trace(Math2.pow(-5,2));

$#@%, it does not work !

Why ? If those static methods are inherited, why we can not call those inherited methods out side the class ? Is it "private" then ? Is it accessible only by the methods defined in the subclass ?


Lets add a line before that script:

trace(Math);
trace(Math2.pow(-5,2));


Wow, it works again ! Those inherited Math methods can be accessed outside the class definition now.

What is the difference with and without "trace(Math)" ?

Pitty ! I am not able to answer this question. AS 2 script is said to be compiled into AS 1 script in the SWF. We understand that public and private methods are created in prototype and static methods are created to the constructor function object. However, we have no "extends" keywword in Flash MX and we can not make static methods inheritance through prototype AS 1. I do not know how MX2004 make inheritance for static methods.

Some argues that, it is because MX2004 does not give superClass the power of activation. If we activate the superClass then the codes of inheritance works as expected. To activate the superClass, we just "use it" in any form. For example:

trace(Math); will activate it.
Math.abs(); will activate it;


OK, lets try an ugly code. We use AS 1 syntax, manually create the inheritance of static methods by assigning its __proto__ to superClass:

class Math2 extends Math {
    static var __proto__ = Math;
    static function squareIt(n:Number) {
        return pow(n, 2);
    }
}

Now everything works like a charm. The subclass now is "equipped" with all those methods of Math. Those methods are now accessible as usual static methods of subclass now. And, we don't need to "activate" the superclass any more.