Re: What about real polymorphism ?? [message #41953 is a reply to message #41951] |
Fri, 10 December 2004 08:21   |
JD Smith
Messages: 850 Registered: December 1999
|
Senior Member |
|
|
On Thu, 2004-12-09 at 18:32 +0100, Antonio Santiago wrote:
>> Do you want:
>>
>> c2->c1::datos
>>
>> ???
>>
>> This will invoke the datos method defined in c1.
>>
>> Karl
>>
>
> Not exactly.
>
> I want to view C2 as a C1 class object but when i invoque "datos" it
> will be execuete the method defined in C2.
> The problem is the point of view. I want to view like in java or other
> OO language but IDL cant accept this point of view :(
>
> With PERSON, MAN and WOMAN is more clear. I want to view a MAN or a
> WOMAN only as a PERSON, something like a cast. Later when i execute
> methods over this PERSON it will execute the "datos" method of WOMEN or
> MAN depending of its subtype (that's polymorphism).
> But i supossed this point of view is not possible in IDL.
It's very possible:
==========================================================
pro Man::AboutMe
print,FORMAT='(%"Dude, I am a %d yr old man, and I like %s.")',self.age, $
self.activity
end
function Man::Init,age
if ~self->Person::Init(age) then return,0
self.activity='watching football'
return,1
end
pro man__define
m={MAN, $
INHERITS PERSON, $
BELT_SIZE: 0.0, $
ACTIVITY: ''}
end
pro Woman::AboutMe
print,FORMAT= $
'(%"Pleased to meet you. I am a %d yr old woman, and I enjoy %s.")', $
self.age, self.activity
end
function Woman::Init,age
if ~self->Person::Init(age) then return,0
self.activity='gardening'
return,1
end
pro woman__define
m={WOMAN, $
INHERITS PERSON, $
SHOE_SIZE: 0.0, $
ACTIVITY: ''}
end
function Person::Init,age
self.age=age
return,1
end
pro person__define
p={PERSON, $
AGE: 0}
end
==========================================================
Compile, and then try:
IDL> people=[obj_new('man',22),obj_new('woman',31)]
IDL> for i=0,1 do people[i]->AboutMe
Dude, I am a 22 yr old man, and I like watching football.
Pleased to meet you. I am a 31 yr old woman, and I enjoy gardening.
Sorry for the flagrant stereo-typing.
JD
|
|
|