Tài liệu Understanding Inheritance - Pdf 87


< Day Day Up >

Understanding Inheritance
A class can gain (inherit) all members from another class. This is called inheritance. The
class that's gaining the members is called a subclass and the class from which it inherits is
called the superclass. If B is a subclass of A, B is said to extend A.
Inheritance promotes code reusability. It allows you to give functionality to other classes
without having to rewrite code that would be redundant. This concept will \be much
clearer by the end of this lesson.
Imagine for a moment that you're writing the ActionScript for a game. This game has a
hero, controlled by the user, and lots of simple enemy characters. The enemies are not the
same as the hero, but they share a number of similarities, such as the ability to walk, get
hurt, heal, and die. If you program separate Hero and Enemy classes, you end up
rewriting much of the same code to handle these common capabilities. So it makes sense
to create a general class that programs these common capabilities—let's call it the
Character class—and then create additional classes (Hero and Enemy) that inherit all the
functionality of the Character class as well as extend that functionality, each in unique
ways. The Hero class might extend the Character class with the capability to be
controlled via a user interface, and the capability to wield many types of weapons. The
Enemy class might extend the Character class with the capability to use an artificial
intelligence algorithm to govern its movement.
In this example, the base superclass (Character) is written once, and two subclasses (Hero
and Enemy) extend the functionality of the base class to make new and unique classes.
Let's look at how this is done.
Let's assume that a Character class has already been created, given properties and
methods, and saved as Character.as. A subclass of the Character class (we'll use the Hero
class) is created by using the keyword extends in the class definition. For example:

class Hero extends Character {


new die() method in the Hero and Enemy classes. Creating a method in a subclass with
the same name as an inherited method from its superclass causes the functionality of the
subclasses' method to take precedence over the inherited method.

Being able to override properties and methods in this way is useful in creating object-
oriented code.
In the following exercise, you'll put into practice most of the concepts you've learned in
this lesson so far. You'll create a class called Animal and program the Animal class with
several capabilities, including running and stopping, as most animals can do. You'll also
create a Cat class that extends Animal by giving Cat animals the capability to meow.
(Because the Cat class extends the Animal class, the Cat class inherits the functionality o
f
the Animal class; Cat animals automatically can run and stop.) In addition, you'll create a
Dog class that extends the Animal class in a manner similar to that of the Cat class,
except that Dog animals will be able to bark. When you've finished scripting these
classes, you'll associate the Cat and Dog classes with different movie clips in the library.
As a result of this association, each instance of one of those clips that you drag into your
project will take on the characteristics of the class with which it's associated.
In this exercise, you'll gain experience creating classes, extending a class, overriding
methods and properties, and working with instances of custom-made classes. Let's get
started!
1. Open PetParade1.fla.
The first order of business is to become familiar with the contents of this FLA.
There are three layers in this project file. The Background layer holds the
background graphics; the Assets layer currently contains six buttons named
dogRun_btn, dogStop_btn, dogSound_btn, catRun_btn, catStop_btn, and
catSound_btn. The Actions layer is currently empty, but will eventually contain
script.

For this exercise, it's also important to understand the assets in the library because

8.
9. function Animal() {
10.
11. this.speed = 5
12.
13. }
14.

The first line gives the class a name, Animal, and then specifies that this class
extends the MovieClip class. This means that instances of the Animal class inherit
all the functionalities (properties and methods) of movie clips—properties such as
_x and _name, as well as methods such as gotoAndPlay() and loadMovie(). This is
your first experience creating a new class that inherits from another class. You can
think of this technique as taking the basic functionality of movie clips and
extending it in a way that's appropriate for programming how Animals work. This
will become clearer as we progress through the steps.
TIP
Any of Flash's built-in classes can be extended in this way. With this capability,
for example, you can create an enhanced Sound, TextField, or Array class to fit
your needs exactly.
The line following the class declaration defines a private variable named speed
that will used by the class. Remember that private variables can only be accessed
and used by scripts within the class definition—not directly by instances of the
class. The speed variable will be used by a method named run() that we'll create in
a moment. This method will be used to move a movie clip based on the value of
speed.
The final three lines of the ActionScript in this step define the constructor method
for the Animal class. Remember that scripts within the constructor are executed at
the moment that an instance of the class is created. Because the Dog and Cat
classes inherit from the Animal class, any scripts placed here will be executed

7. function stop() {
8.
9. delete this.onEnterFrame;
10.
11. }
12.

This method stops the instance from moving, simply by deleting the onEnterFrame
event from the instance.
6. End the definition of the Animal class with a closing curly brace (}); then choose
File > Save to save the class file.

You've created the Animal class. Next you'll create the two subclasses (Dog and
Cat) that extend Animal.
7. In the same directory as Animal.as, create a new ActionScript file named Cat.as.
Start the class definition with the following script:
8.
9. class Cat extends Animal {
10.

The first two words tell Flash that you're creating a new class called Cat. The next
two words, extends Animal, tell Flash that this class inherits every method and
property of the Animal class, including run() and stop(). We used similar syntax
when defining the Animal class; it extended the MovieClip class. As a result, not
only does the Cat class get all the capabilities of the Animal class, but those of the
MovieClip class as well. Inheritance trickles down like this as long as you
continue extending classes.


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status