HyperCard OOP
Volume Number: 6
Issue Number: 8
Column Tag: HyperChat™
Object Oriented Programming
By Tom Trinko, Ph.D., Colleen Trinko, Fremont, CA
Note: Source code files accompanying article are located on MacTech CD-ROM or
source code disks.
Object Oriented Programming in HyperCard™
While HyperCard provides a rich set of tools for developing applications and for
rapidly prototyping complex systems, it is not a true object oriented computing
environment. We’re not going to go into a detailed discussion of object oriented
programming, but a little overview, with an example of creating pop-up menus,
should illuminate the basic concepts for those who haven’t had a chance to become
familiar with this relatively new approach to developing, and thinking about, software.
The first key concept in object oriented programming is that the elements of the
program interact by sending each other messages. These messages cause the recipient
to take certain actions. The same message can cause different actions to occur
depending upon the identity of the message recipient. For example, if one says “Hi” to
a dog it might bark, while if you say “Hi” to a friend they are unlikely to bark, unless
of course the friend is a programmer in which case anything is possible. The
important point is that how the recipient responds is dependent upon the nature of the
recipient; the sender does not need to be aware of the internal details of the target of its
message. In HyperTalk each “on something....end something” message handler defines
how an object-- button, field, card, background, stack, home--will respond to a
message. So if you were to call one field a man and one a dog you could define two
different handlers called “on hi....end hi” to describe their behaviors. The behavior of
an object is also affected by its instance variables. Instance variables are just the
values which describe the object’s characteristics. For example an instance variable
for a dog might be its hair color. All dogs have a “slot” for this value but each
specific dog has a different value. Another instance variable would be the dog’s
proclivity to barking. The instance variable would reflect how likely a dog is to bark
and would be used by the “on hi...end hi” handler as illustrated in this script:
--1
on hi
global barkingprobability
if the random of 100 < barkingprobability then
put “woof” into message
play “woof”
end if
end hi
This object oriented approach lets you develop your program as a set of
self-contained elements whose activities and interfaces with the other objects in your
system are well defined. It’s an approach which lends itself to rapidly developing
complex systems as well as supporting the development of reusable software. Once
you’ve defined an object that performs a certain function you can use it in any program
which needs that ability.
The second key concept is the concept of a class structure. This simply means you
have a hierarchy of objects with subclasses of any class inheriting the characteristics
of their parent class. A simple example would be if you were to model a zoo. Your root