classes (was Re: Same again please for OOP)
roy at panix.com
Sun Dec 24 19:32:42 CET 2000
"Alex Martelli" <aleaxit at yahoo.com> wrote:
> For example, suppose our program will need to model CD's. A CD
> object has several attributes -- depending on our purposes, we may
> want to model some appropriate subset of them; for example, a
> "title", a "price" in Euros, and a sequence of "tracks" (each of which
> has its own attributes, say a "title" and a "duration" in seconds).
That's good as far as it goes, but I think you missed the real power of
classes, which is subclassing.
What you really want to do is define a class AudioStorage. It could be
essentially identical to your CD class, but you could then have
subclasses such as CD, Vinyl, DAT, DDT, mp3s_in_a_tar_file, etc.
The Vinyl sub-class, for examle, might want to keep track of which
tracks are on the A-side and which are on the B-side, and throw an
AudioStorage.offline exception when you try to call its play() method.
It might also have an rpm attribute, which can take on the values 33,
45, or 78.
The mp3 class might return a URL for its location() method, and the CD
or Vinyl classes might return a string, "Third shelf from the bottom,
near the left end".
More information about the Python-list