Java Reference
In-Depth Information
You might have a parameter
owner
, which would record the owner's name, so the value stored as the at-
tribute
owner
could be changed when the hat was sold or otherwise transferred to someone else. You might
also have a parameter
hatOn
, for example, which would indicate whether the hat was on or off the owner's
head; the value
true
would indicate that the owner was indeed wearing the hat, whereas the value
false
would mean that the hat had been removed and was just lying about somewhere.
Operating on Objects
In spite of what you might think from looking at
Figure 1-5
, a class object is not just a collection of various
items of data. In addition to the parameters that characterize an object, a class specifies what you can do with
an object — that is, it defines the operations that are possible on objects of the class. Clearly, for objects to
be of any use in a program, you need to decide what you can do with them. The operations that you specify
for objects of a given type depend on what sort of objects you are talking about, the attributes they contain,
and how you intend to use them.
putHatOn
and
takeHatOff
, which would have meanings that are fairly obvious from their names, and do
make sense for
CowboyHat
objects. These operations on a particular
CowboyHat
object would set the value
of
hatOn
for the object. To determine whether your
CowboyHat
was on or off, you would just need to look
at this value. Conceivably, you might also have an operation
changeOwner
by which you could set the in-
stance variable recording the current owner's name to a new value.
Figure 1-6
shows two operations applied
in succession to a
CowboyHat
object.