tutorials flash transtypage
Transtypage (or polymorphism)
`Polymorphism' or “dynamic casting” exists in all the OOP languages which use strong typing and ActionScript is no exception since its use since FlashMX2004 and the compilation in AS2.
It sometimes happens that we use polymorphism without thinking too much about it and in the end it is often very practical to be able to force the ActionScript compiler to demand strongly typed variables.
Here are 2 examples of polymorphism in AS2 (valid in AS3) to illustrate the use of this technique:
1 - First technique using inheritance
// AS2 code class A { // ----o constructor public function A() { // } // ----o Public Methods public function methodA():Void { trace("methodA") ; } } class B extends A { // ----o Constructor public function B () { // } // ----o Public Methods public function methodB():Void { trace("methodB") ; } } // main code var a:A = new B() ; // a.methodB() ; // error compiler here. B(a).methodB() ; // works perfectly with the "dynamic cast"
In the example above the ‘a’ variable is type ‘A’ and will be instanced with the class B which inherits from A. So this is not a problem for the compiler. Unless you want to then directly use a class B method on the instance, this method will be impossible! We use this technique to cast a variable and to limit it if possible to a strict use of methods from its super class.
You can see above that it is fairly simple to cast the variable by using polymorphism B (a) and therefore it is possible to use the “methodB” method of the B class on the instance.
It sometimes happens that we don’t know in advance if an object will be defined with one class or another and it is great to be able to rely on a super class (which can be abstract) to declare certain functionalities and to ensure good functioning of the code thereafter. Now if you need to use a method specific to a class which inherits the defined type at the beginning then polymorphism is very important.
2 - Second technique with the use of an interface
// AS2 code interface IA { function methodA1():* ; } class A implements IA { // ----o constructor public function A() { // } // ----o Public Methods public function methodA1():Void { trace("methodA1") ; } public function methodA2():Void { trace("methodA2") ; } } // Test var a:IA = new A() ; a.methodA1() ; // no error, the compiler finds the method. // a.methodA2() ; // compiler error A(a).methodA2() ; // "dynamic cast" on the variable, all works correctly now
We get the same sort of problems as technique 1. The methodA2 method is not defined in the IA interface, but I want to use this method on my typed IA variable which instances class A and which implements IA. Polymorphism here functions perfectly and calling the method works without any problems.
You may be wondering why I have written this article! And why I have given it an AS3 label :)
Quite simply because in AS3 there is a new keyword: ‘as’ which makes it possible in my opinion to make a dynamic cast which is even more elegant than we could do in AS2….
I think that an example would be much more interesting than a long discussion, I am again using the example above with the use of an interface and this time I am using the ‘as’ to resolve a typing problem.
// AS3 code in your file containing your Main class! package { class MainClass { } } interface IA { function methodA1():void ; } class A implements IA { // ----o constructor public function A() { // } // ----o Public Methods public function methodA1():void { trace("methodA1") ; } public function methodA2():void { trace("methodA2") ; } } // Test var a:IA = new A() ; a.methodA1() ; (a as A).methodA2() ; // "dynamic cast" an use of as
You can quickly test your code as I have just done in the main file of your “main” class of your AS3 project in Flex2. You just need to put the code outside the package to test it!! It works really well for quick tests on the Flash timeline, no need to always code in the main class, it’s great!
In conclusion, with the arrival of the key words ‘is’ and ‘as’ we can say that AS3 has 2 quite useful tools. I advise you to read anything that relates to these two key words relating to AS3 language because even if they seem quite unimportant, I think that you will end up using them a lot like me.
By EKAMELEON - ALCARAZ Marc (2006) You can find this tutorial and comments on the subject on my blog
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42


