On Mon, Oct 10, 2011 at 18:59, Kevin E. [email protected] wrote:
“type” is a variable that differentiates the zone instances so
that they can have different behaviors.
That’s what making subclasses is usually used for.
For example if you need an area() function, you might make your Zone
class uninstantiatable (what in C++ would call an “abstract class”,
and Java would call an “interface”. (Or you could have a default Zone
shape, like say maybe a line, so its area would be a constant zero –
idealized line, zero thickness.) Then you could create subclasses
like Square, Circle, EquilateralTriangle, Hexagon, and so on, where
you’d only need to know one given dimension, which would probably be
the length of a side of a polygon, and maybe the radius of a circle.
Then you’d “override” (change the definition of) the area function, to
apply the correct formula for that shape.
let’s skip most of it
class Square < Zone
again, let’s skip most of it
length * length
class Circle < Zone
once more, let’s skip most of it
Math.PI * length * length
So then you could have an object that you know is a Zone… but you
don’t know (nor care!) whether it’s just a Zone, or whether it’s
some subclass like Square or Circle or whatever, you can still call
area() on it, and get back an answer. As in:
z = CreateRandomZone
a = z.area
“length” is a dimension of a zone.
This part makes sense though. Class instances (i.e., objects) usually
do have some attributes, properties, or whatever you want to call
them. There are things that vary according to which instance it is
(i.e., which Square, or which Circle), which become instance
attributes. Some vary according to which class (like, for the
polygonal subclasses, might be something like number_of_sides) it is,
and become class attributes (since they don’t need to be stored on