We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
OO question (Read 973 times)
OO question
May 18th, 2005, 1:56am
 
I'm still getting to grips with object-oriented programming and need some advice.

In several sketches now I have used a structure with one class instantiating arrays of one or more other class of objects- seemed like a good way of keeping things organised.. problem is that the objects in the arrays can only access data/methods of their parent object by using its name as defined in the main sketch.. this was fine when only using one instance but now i want to use more and so this approach is rather clunky.

Is there a way for an object to access its parent? or a better way to structure things?

I have asked basically the same question before but I hope this is a bit clearer. I can post code if that would help. As usual any assistance, pointers etc. is hugely appreciated.

Cheers,
Sam
Re: OO question
Reply #1 - May 18th, 2005, 2:34am
 
[imNotQuiteSureThisIsRight]

the way the java scope works is that an object that is initiated in one will have access to all its parent's functions and variables
[crappyASCIIdrawing]
Code:

_______________
| Parent |
| _________ |
| | Child 1| |
| |_________| |
| _________ |
| | Child 2| |
| |_________| |
|_______________|

[/crappyASCIIdrawing]

now, Child1 has access to Parent's stuff, and so does Child2, but Child1 does not have direct access to Child2, though it does have access to it through Parent. To access a variable you just use it like you would in the parent, no suffixes or anything fancy.

[/imNotQuiteSureThisIsRight]
Re: OO question
Reply #2 - May 18th, 2005, 8:39am
 
in flash i would pass a reference to the parent object as an argument to be picked up by the child object's constructor and stored in a object property.. heh, but im completely new to processing and java (and pretty new to OOP too).. sorry for being useless Smiley
Re: OO question
Reply #3 - May 18th, 2005, 1:48pm
 
Sam, I'm not quite sure what the problem is. You do have access to all methods and variables of the main class directly from all other classes you define. If you need one object to be able to access the data of another object the "nicest" way is to pass it as a parameter to a function, i.e.:

Code:
class Obj1 {
float x,y;
}

class Obj2 {
float x,y;

void addPosition(Obj1 o) {
x+=o.x;
y+=o.y;
}
}


If the classes really function as a parent-child hierarchy the best solution would be to use a parent reference variable and passing that to the child object as part of the constructor.

Code:
class Parent {
float x,y;
}

class Child {
Parent parent;
float x,y;

public Child(Parent theParent) {
parent=theParent;
x=parent.x;
y=parent.y;
}
}


Mythmon's sketch is right, but the correct way to describe it would be to say that a local class has access to all data of the class it is defined in. In Processing, any class you define is actually a local class of the class that is your sketch, which again extends processing.core.PApplet. Most of the time you don't need to know that, though.
Re: OO question
Reply #4 - May 22nd, 2005, 3:18pm
 
Thanks mythmon and marius for your replies. I suspected things should work as you described but have had problems in the past- probably from other code- and fudged around them, hence my clunky approach. Your explanations have made the situation a lot clearer- guess i will rework my sketches (and do things properly this time).

Cheers,
Sam
Page Index Toggle Pages: 1