302
Inheritance
Your subclass typically defines additional methods and properties that you can use in your
application, hence it
extends
the superclass. Subclasses can also override (provide their own
definitions for) methods inherited from a superclass. If a subclass overrides a method
inherited from its superclass, you can no longer access the superclass’s definition within the
subclass. The only exception to the above rule is that, if you are within the subclass’s
constructor function, you call the superclass’s constructor using the
super
statement. For
more information on overriding, see
“Overriding methods and properties” on page 306
.
For example, you might create a Mammal class that defines certain properties and behaviors
that are common to all mammals. You could then create a Cat subclass that extends the
Mammal class. Using subclasses lets you reuse code so that instead of re-creating all the code
common to both classes you could simply extend an existing class. Another subclass, the
Siamese class, could extend the Cat class, and so on. In a complex application, determining
how to structure the hierarchy of your classes is a large part of the design process.
Inheritance and subclassing are very useful in larger applications, because they let you create a
series of related classes that can share functionality. For example, you could create an
Employee class that defines the basic methods and properties of a typical employee within a
company. You could then create a new class called Contractor that extends the Employee class
and inherits all of its methods and properties. The Contractor class could add its own specific
methods and properties, or it could override methods and properties that are defined in the
Employee superclass. You could then create a new class called Manager, which also extends
the Employee class and defines additional methods and properties such as
hire()
,
fire()
,
raise()
, and
promote()
. You could even extend a subclass, such as Manager, and create a
new class called Director, which again adds new methods or overrides existing methods.
Each time that you extend an existing class, the new class inherits all the current methods and
properties of the subclass. If each class wasn’t related, you’d have to rewrite each method and
property in each separate class file, even if the functionality was the same in the fellow classes.
You would have to spend a lot more time not only coding, but also debugging your
application and maintaining a project if similar logic changed in multiple files.
In ActionScript, you use the
extends
keyword to establish inheritance between a class and its
superclass, or to extend an interface. For more information on using the
extends
keyword,
see
“About writing subclasses in Flash” on page 303
and
“About writing a subclass”
on page 303
. For additional information on the
extends
keyword, see extends
statement
in the
ActionScript 2.0 Language Reference
.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...