When is a subclass not right?
Gabriel Genellina
gagsl-py at yahoo.com.ar
Thu Aug 24 16:21:21 EDT 2006
At Thursday 24/8/2006 16:23, Chaz Ginger wrote:
>I was writing some code that used someone else class as a subclass. He
>wrote me to tell me that using his class as a subclass was incorrect. I
>am wondering under what conditions, if ever, does a class using a
>subclass not work.
>
>class B1(A);
> def __init__(self,a1,a2) :
> self.c = a1
> A.__init__(self,ag)
>
>class B2:
> def __init__(self,a1,a2):
> self.c = a1
> self.t = A(a2)
>
> def bar(self) :
> self.t.bar()
>
>Other than the obvious difference of B2 having an attribute 't', I can't
>see any other obvious differences. Is there something I am missing?
Look any OO book for the difference between 'inheritance' and
'delegation'. In short, you should inherit when B 'is an' A (a Car is
a Vehicle), and delegate/compose in other cases (a Car has an Engine;
or more precisely, a Car instance has an Engine instance).
Gabriel Genellina
Softlab SRL
p5.vert.ukl.yahoo.com uncompressed Thu Aug 24 19:27:05 GMT 2006
__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas
More information about the Python-list
mailing list