At Thursday 24/8/2006 17:44, 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).
That is merely a logical use of OO after all when would a car and an
orange be the same?
Uh... what's the point...?
By example, an orange inside a car would be modeled using
composition, never inheritance.
I was wondering more about the mechanics of Python: when does B1 show
different characteristics than B2 (forgoing the obvious simple things,
like 't' above).
Inheritance implies that *all* methods/attributes of A are exposed by
B1; it's directly supported by the language. Inheritance is usually a
relationship between classes. If you add a method foo() to A,
instances of B1 automatically have it. A B1 instance "is an" A instance.
Using delegation, you have to delegate the desired method calls
yourself (but there are ways to do that automatically, too).
Delegation is a relationship between instances. If you add a method
foo() to A, you have to add it to B2 too. A B2 instance "is not an" A instance.
Gabriel Genellina
Softlab SRL
p4.vert.ukl.yahoo.com uncompressed Thu Aug 24 21: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
--
http://mail.python.org/mailman/listinfo/python-list