"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.
For instance:
class Zone
# let's skip most of it
def area
0
end
end
class Square < Zone
# again, let's skip most of it
def area
length * length
end
end
class Circle < Zone
# once more, let's skip most of it
def area
Math.PI * length * length
end
end
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
each instance).
-Dave
···
On Mon, Oct 10, 2011 at 18:59, Kevin E. <kellwood@gmail.com> wrote:
--
LOOKING FOR WORK, preferably Ruby on Rails, in NoVa/DC; see main web site.
Main Web Site: davearonson.com
Programming Blog: codosaur.us
Excellence Blog: dare2xl.com