About object-oriented programming and Flash
233
One of the major benefits of using a superclass/subclass structure is that it is easier to reuse
similar code between various classes. For example, you could build a superclass called Animal,
which contains common characteristics and behaviors of all animals. Next you could build
several subclasses that inherit from the Animal superclass and add characteristics and
behaviors specific to that type of animal.
You might create a Cat class that inherits from another class. For example, you might create a
Mammal class that defines certain properties and behaviors common to all mammals. You
could then create a Cat subclass that extends the Mammal class. Another subclass, say, the
Siamese class, could extend (
subclass
) the Cat class, and so on.
Writing subclasses lets you reuse code. Instead of recreating all the code common to both
classes, you can simply extend an existing class.
For more information on inheritance and subclasses, see
Chapter 8, “Inheritance,” on
page 301
.
Interfaces
Interfaces
in OOP can be described as templates of class definitions, and classes that
implement interfaces are required to implement that template of methods. Using the cat
analogy, an interface is similar to a blueprint of a cat: the blueprint tells you which parts you
need, but not necessarily how those parts are assembled, or how the parts work.
You can use interfaces to add structure and ease of maintenance to your applications. Because
ActionScript 2.0 supports extending only from a single superclass, you can use interfaces as a
form of limited multiple inheritance.
You can also think of an interface as a “programming contract” that you can use to enforce
relationships between otherwise unrelated classes. For example, suppose you are working with
a team of programmers, each of whom is working on a different part (class) of the same
application. While designing the application, you agree on a set of methods that the different
classes use to communicate. So you create an interface that declares these methods, their
parameters, and their return types. Any class that implements this interface must provide
definitions for those methods; otherwise, a compiler error results.
For more information on inheritance, see
Chapter 8, “Inheritance,” on page 301
. For more
information on interfaces, see
Chapter 9, “Interfaces,” on page 313
.
TIP
In a complex application, determining how to structure the hierarchy of your classes is an
important part of the design process. Make sure you determine this hierarchy before you
begin to program.
Содержание 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...