[Tutor] Classes, Classes, Classes
Fri, 12 Nov 1999 10:22:24 -0600
Maybe it's just because I'm bored and thinking a nice game of Q2 would cure
that ailment, here's another way to look at classes in a fairly high level
In your typical game of Quake(II) you encounter many different types of
monsters to which you are suppose to blow to kingdom come. Each of these
mosters has specific attributes about them... They attack in different ways,
have different weapons at their disposal, etc. However, each class of monster
has many things in common. So, the best way to write a program such as that,
would be to use a method where you can construct a general form of monster. For
instance, we'll take your common enemy, the sentry guy. Since all sentry guys
are going to look the same (initially), have the same weapon, and same attack
methods, having some sort of plan for making these monsters quickly would be
nice. Classes do that.... They let you define all the details of the monster...
Then, you create the individual monsters from that plan.... After which, if one
monster is killed by you, you might have a variable named "dead", which would
basically be a boolean (true/false) sort of variable. Also, since the monsters
can attack you independantly of each other, the game might call an attack
function/method of one monster, whille calling the function/method for causing
another one to run away.
I always liked the description of subclassing from the "Teach Yourself Java in
21 Days".... Say you want to create a bunch of monsters. Well, what do all
monsters have in common? Maybe that they're scary, mean, and generally
anti-social. So, you create a real general class called Monster. Now, some
monsters have 4 legs, some have 2. So, you create a somewhat more specific
class for 2 legged monsters, and for 4 legged monsters. Each, however, sharing
the basic attributes of all monsters, so they are subclasses of the Monster
class. Maybe some 4-legged monsters can fly, while others can't. So, under the
4-legged monster class, you can create a somewhat more specific set of classes.
One for the 4-legged monsters that can fly, and one for 4-legged monsters that
Subclassing is basically starting with a real general subject/topic (Monsters),
and then becoming increasingly specific (2/4 legs, can fly, can't fly). So,
another example might be the general topic of birds. All birds have feathers,
and have two legs. So, we might make a class:
legs = 2
haveFeathers = 1
Now, some birds can fly, so you might make a subclass for birds that can fly,
and another for those that can't. Under those that don't fly, you might include
more specific things, such as a class named Chickens. Having methods like
layEgg(), runAroundWithHeadChoppedOff(), etc.
> RP> Hello, I am experiencing trouble understanding classes. I don't
> RP> understand how they work, what they do and their purpose in
> RP> general.
> Classes are used to group data together with the functions (methods) that
> operate on them. The usual intention is that an object holds a number of
> pieces of data and a functional interface (API) to those data are presented
> to the world. Since the data can change as requirements change (say, as new
> algorithms are needed to improve performance), hiding the data
> representation behind an API reduces the coupling between the class and the
> code that uses it.
> Perhaps the simplest example of this is the use of polar coordinates
> vs. cartesian coordinates to represent points in 2D or 3D space.
[ ... ]