|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
MXDJ TOP LINKS YOU MUST CLICK ON ! Tips From The Masters Multiple Inheritance in ActionScript 3.0
Despite popular belief that it's not supported, multiple inheritance is actually possible with AS3
By: Darron J. Schall
Dec. 20, 2006 01:00 PM
Did you know ActionScript 3.0 supports multiple inheritance? Here's how... Multiple inheritance is actually possible with AS3, despite popular belief that it's not supported. It's not really multiple inheritance in the true sense of the word going by the strict definition... but there's a way to get the job done in AS3 that behaves almost like the real thing.
Various disclaimers aside, here we go... When you simulate the use multiple inheritance, there are three things you need to do:
Did you forget that ActionScript 3.0 is a dynamic language and supports a long-since-forgotten-about-since-ActionScript-1-days but still-supported-because-its-in-the-spec #include pragma? This is where it gets ugly, but stay with me. We'll create an interface, and then write the implementation for that interface in a free-standing .as file that contains just loose ActionScript code. Then, like magic, we'll include that file in our class and have the compiler automatically insert the code. The result? A class that implements an interface, with a default implementation that's separate from the class, and re-usable through many classes. Here's a concrete example to further drive home the idea:
// In CartoonCharacter.as From the above series of code blocks, hopefully you can see what happened. In our "impl" file we just have a method name and a method body. The method body is the default implementation for the CartoonCharacter interface. In our CartoonDog and CartoonCat classes, we include that default implementation and implement the interface. If you ask the dog if he's a CartoonCharacter, he'll surely respond that he is. Likewise with the cat...
if ( dog is CartoonCharacter ) ... but by having the implementation in a separate ActionScript file, it allows us to pretend that both dog and cat inherit from CartoonCharacter. When we change the "impl" file, we change the behavior for all of the classes that #include the file. This is important because in this use case, we have to extend a display object class so we can be added on screen. Now, I know the first question will be "Why not just make CartoonCharacter a class that extends Sprite and have Dog and Cat extend that?" to which I say, "you completely missed the point of this article." All that aside, there are a few gotchas to using this technique. In no particular order:
Anyway, like I said, use with caution. There are definitely cases where this approach works well, and there are other cases where it's better to change your architecture and avoid it. I leave it as a thought exercise to you to know when to use it and when not to. Don't go crazy, kids, you might want to keep the training wheels on for this one... LATEST FLEX STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||