[Tutor] Multiple inheritance - Need clarity
Dennis Lee Bieber
wlfraed at ix.netcom.com
Fri Nov 26 22:46:56 EST 2021
o/~ Talking to myself in public o/~
On Fri, 26 Nov 2021 22:14:18 -0500, Dennis Lee Bieber
<wlfraed at ix.netcom.com> declaimed the following:
> Personally, I would not use inheritance for this example but
>composition.
>
To better clarify...
Inheritance is used if/when the subclass IS also a parent class
A Cylinder is NOT a Circle, NOR is it a Rectangle.
Class Carnivore():
...
Class Mammal():
...
Class Fish():
...
Class Cat(Mammal, Carnivore):
...
Class Shark(Fish, Carnivore):
...
A Cat IS A Mammal.
A Cat IS also A Carnivore.
A Shark IS A Fish.
A Shark IS also A Carnivore.
Composition is used when the class is made up of other objects.
A Cylinder HAS A Circle (actually, two identical for top and bottom), A
Cylinder also HAS A Rectangle (using your style for the column)
Class Vehicle():
...
Class Wheel():
...
Class Engine():
...
Class InternalCombustion(Engine):
#internal combustion IS A engine
Class Diesel(Engine):
#similarly
Class PassengerCar():
def __init__(self, engine):
self.wheels = { "LF" : Wheel(...),
"RF": Wheel(...),
"LR" : Wheel(...),
"RR" : Wheel(...) ]
self.engine = engine
A PassengerCar HAS multiple Wheels, a PassengerCar HAS an engine.
(some sports cars may have larger rear tires than front tires -- I've only
hinted that this could be the case her -- would have to add parameters to
__init__ to allow for such.
Personally, I wouldn't even use composition for your Cylinder() -- I'd
just save radius and height directly, rather than the indirect radius (in
Circle()) and height/width (in Rectangle) -- do you expect anyone to ask
for
cylinder.rectangle.width
?
I'd think anyone working with a cylinder would be more likely to ask for
cylinder.radius
cylinder.height
NOT
cylinder.circle.radius
cylinder.rectangle.height
(who thinks of /rectangles/ when discussing a cylinder?) Even worse, your
rectangle just has side1 and side2 -- how does a user know which is the
height, and which is the width?
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
More information about the Tutor
mailing list