ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Inheritance | 25

class inherits everything in theSpriteclass, the application should still work. As you


will see, it works just fine. The reason thatSpriteis used instead ofMovieClipis that


we did not want to have any unnecessary baggage—just the minimum we needed. If


you changeSpriteto the next class it inherits from,DisplayObjectContainer, you will


see that the application fails. This means that the application requires one of the


Sprite class properties that isnot inherited.


One byte over the line: in Example 1-9, if you substitute theMovieClip
forSpriteclasses for the parent class, you will find that your SWF file
is larger than when you tested it withSprite(708 bytes versus 679
bytes). The 29 byte difference probably won’t bloat your program sig-
nificantly, but with added classes in design pattern applications, an
unnecessary byte here and one there might add up. (When you win a
contract because your application was 1 byte less than the competi-
tion’s, you’ll be glad we had this little chat.)

In addition to seeing what is inherited in ActionScript 3.0 in theLanguage Refer-


ence, you might also want to note what’s in the packages you import. If you import


flash.display.*you can bring in everything in thedisplaypackage. That’s why


importing just what you need, e.g.,flash.display.Spriteorflash.display.Shape,is


far more frugal and less cluttering.


Writing Classes for Inheritance


As can be seen from looking at the inheritance structure of ActionScript 3.0, a well-


planned application benefits greatly from a well-organized plan of inheritance. To


see how inheritance works from the inside, the next example provides a simple


inheritance model for our four-legged friends. Even with this simple example, you


can see what is inherited and what is unique to an application.


Example 1-13 through Example 1-16 make up the application illustrating inherit-


ance. The first class,QuadPets, is the parent or superclass with a constructor that


indicates when an instance of the class is instantiated using atracestatement. Any


class that inherits theQuadPets class gets all of its methods and interfaces.


Example 1-13. QuadPets.as


package
{
public class QuadPets
{
public function QuadPets( ):void
{
trace("QuadPets is instantiated");
}
public function makeSound( ):void
{

Free download pdf