Accessing in a child class the attribute of a parent class

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Accessing in a child class the attribute of a parent class

luciano de souza-2
Hi all,

I was trying to create a graphical interface using IUP with object
oriented programming. Something wrong occurs. I prefer to simplify the
example to ilustrate what is happining.

In the original code, the class "parent" defines the window general
behavior and the child class is a form inheriting methods and
attributes. I prefer to simplify because the quantity of code.

But the problem is the same: how to access in child class an attribute
created in the parent?

the result of 14th line is "nil", whereas I expected 15 as the answer. I
tried self.a in the place of parent.a, but the result is still "nil". I
tried to declare in the 5th line "parent.a", but the result is the same.

01 require('classlib')
02
03 class.parent()
04 function parent:__init()
05 self.a = 15
06 end
07
08 class.child()
09 function child:__init()
10 self.b = 22
11 end
12
13 function child:display()
14 print(parent.a)
15 print(self.b)
16 end
17
18 x = child()
19 x:display()

Regarding child should receive methods and attributes from parent, for
me, the most reasonable idea would be to call in child: "self.a". the
attribute originally declared in parent would have been delivered to
child class. If the attribute was created as "parent.a" is an atribute
from the class, if the sintax is "self.a" is an attribute from the object.

in other words, I use "parent.a" in 14th line only because it works in
another similar code. Actually, I imagined the correct call is "self.a".

What's wrong? How to call the parent attribute and receive 15 as an answer?

I send classlib together only because it is declared. However, regarding
it's a standard module, I don't believe the error is inside it. The
problem is certainly conceptual.

Luciano de Souza

example.lua (236 bytes) Download Attachment
classlib.lua (11K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Accessing in a child class the attribute of a parent class

Norbert Kiesel
On Wed, 2011-01-05 at 21:03 -0200, Luciano de Souza wrote:

> Hi all,
>
> I was trying to create a graphical interface using IUP with object
> oriented programming. Something wrong occurs. I prefer to simplify the
> example to ilustrate what is happining.
>
> In the original code, the class "parent" defines the window general
> behavior and the child class is a form inheriting methods and
> attributes. I prefer to simplify because the quantity of code.
>
> But the problem is the same: how to access in child class an attribute
> created in the parent?
>
> the result of 14th line is "nil", whereas I expected 15 as the answer. I
> tried self.a in the place of parent.a, but the result is still "nil". I
> tried to declare in the 5th line "parent.a", but the result is the same.
>
> 01 require('classlib')
> 02
> 03 class.parent()
> 04 function parent:__init()
> 05 self.a = 15
> 06 end
> 07
> 08 class.child()
> 09 function child:__init()
> 10 self.b = 22
> 11 end
> 12
> 13 function child:display()
> 14 print(parent.a)
> 15 print(self.b)
> 16 end
> 17
> 18 x = child()
> 19 x:display()

Barring any black magic in classlib: where is defined that child is a
subclass of parent?




Reply | Threaded
Open this post in threaded view
|

Re: Accessing in a child class the attribute of a parent class

luciano de souza-2
Unforgivable! It's difficult to explain how could I forget it!

The line 08 should be replaced from "class.child()", a simple class, to
"class.child(parent)", subclass inheriting attributes and methods of
"parent".

But, despite this mistake, the problem related in the previous message
is still occuring with the same exact diagnosis!

On 05-01-2011 21:23, Norbert Kiesel wrote:

On Wed, 2011-01-05 at 21:03 -0200, Luciano de Souza wrote:

> Hi all,
>
> I was trying to create a graphical interface using IUP with object
> oriented programming. Something wrong occurs. I prefer to simplify the
> example to ilustrate what is happining.
>
> In the original code, the class "parent" defines the window general
> behavior and the child class is a form inheriting methods and
> attributes. I prefer to simplify because the quantity of code.
>
> But the problem is the same: how to access in child class an attribute
> created in the parent?
>
> the result of 14th line is "nil", whereas I expected 15 as the answer. I
> tried self.a in the place of parent.a, but the result is still "nil". I
> tried to declare in the 5th line "parent.a", but the result is the same.
>
> 01 require('classlib')
> 02
> 03 class.parent()
> 04 function parent:__init()
> 05 self.a = 15
> 06 end
> 07
> 08 class.child()
> 09 function child:__init()
> 10 self.b = 22
> 11 end
> 12
> 13 function child:display()
> 14 print(parent.a)
> 15 print(self.b)
> 16 end
> 17
> 18 x = child()
> 19 x:display()
>    
Barring any black magic in classlib: where is defined that child is a
subclass of parent?







Reply | Threaded
Open this post in threaded view
|

Re: Accessing in a child class the attribute of a parent class

Tomás Guisasola-2
  Hi Luciano

> The line 08 should be replaced from "class.child()", a simple class, to
> "class.child(parent)", subclass inheriting attributes and methods of
> "parent".
>
> But, despite this mistake, the problem related in the previous message is
> still occuring with the same exact diagnosis!
  I noticed the mistake and changed the code as you mentioned above.
Then I renamed the __init methods to __user_init, but in both versions
the problem still occurs.  I can't understand why, but classlib is
calling child:__init (or __user_init) before calling parent:__init (or
__user_init).  Worst, it is calling the functions with different values
of self, thus the attributes are initialized in different objects :-(
  I think you have to study classlib to understand how it should
be used.

  Regards,
  Tomás
Reply | Threaded
Open this post in threaded view
|

Re: Accessing in a child class the attribute of a parent class

luciano de souza-2
I really get angry with myself when the problem is the lack of
attention. Sometimes, reading rapidly with the screen reader, some
information were ignored by brain, especially because the hurry. Your
are right. the problem is the bad usage of classlib.

The correct code is:


require('classlib')

class.parent()
function parent:__init()
self.a = 15
end

class.child(shared(parent))
function child:__init()
parent:__init()
self.b = 22
end

function child:display()
print(parent.a)
print(self.b)
end

x = child()
x:display()

On 05-01-2011 21:43, Tomas Guisasola Gorham wrote:    Hi Luciano

> The line 08 should be replaced from "class.child()", a simple class,
> to "class.child(parent)", subclass inheriting attributes and methods
> of "parent".
>
> But, despite this mistake, the problem related in the previous message
> is still occuring with the same exact diagnosis!
     I noticed the mistake and changed the code as you mentioned above.
Then I renamed the __init methods to __user_init, but in both versions
the problem still occurs.  I can't understand why, but classlib is
calling child:__init (or __user_init) before calling parent:__init (or
__user_init).  Worst, it is calling the functions with different values
of self, thus the attributes are initialized in different objects :-(
     I think you have to study classlib to understand how it should
be used.

     Regards,
         Tomás


Reply | Threaded
Open this post in threaded view
|

Re: Accessing in a child class the attribute of a parent class

Tomás Guisasola-2
  Hi Luciano

> class.child(shared(parent))
> function child:__init()
> parent:__init()
> self.b = 22
> end

  Seems strange that you have to write both that child inherits
from (shared) parent and also calls the __init method from parent...
Will the call to parent:__init() be sufficient?

  Regards,
  Tomás