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.
IndexProgramming Questions & HelpPrograms › OOP Question passing variables
Page Index Toggle Pages: 1
OOP Question passing variables (Read 1615 times)
OOP Question passing variables
Sep 29th, 2009, 6:54pm
 
Just got a OOP question i cant answer myself.

just to make an example,
i want 3 areas, inside these areas i want a lot of balls. so i first created the area class that contains alot of variables, like size, position, colour etc. of the area.  Now i create another class and create an array of them in every area-class. ok, works fine so far. But now my main question is, how can i use all the variables of the corresponding areaclass in the ballclass , for example to color them the same as the area?

i would normally pass them in the contructor, but is that the only way?
does extending make sense in this case? if i understood right, no...

So im just curious what the best way to do this would be. i dont wanna write a too long contructor. and i am asking myself if doing this is a bit redundant
Re: OOP Question passing variables
Reply #1 - Sep 29th, 2009, 10:58pm
 
you can also write the ball class inside the area class:

class area{
 area vars
 area construct, update, etc
 ball[] (area owns its balls)
 
 class ball{
   ball vars (inherits area vars)
   ball construct, update, etc
 }
}

--Ben
Re: OOP Question passing variables
Reply #2 - Sep 30th, 2009, 2:17am
 
Ah ok, i thought thats what i did and was wondering why it didnt work. But i just created and called the ball class within the area class. I didnt know it actually has to be written inside the other class to work. but makes sense and works now. Thanks!
Re: OOP Question passing variables
Reply #3 - Sep 30th, 2009, 2:27am
 
Not sure if I fully understood the whole problem, so I will try and answer rather generically.
The Area and Ball classes can co-exist side-by-side. That's the most classical way to do that. In this case, the balls can know the area they belong to by adding a reference to the area.

Here is a possible way to do that:
Code:
class Area
{
 int width, height; color back;
 // other stuff...
 Ball[] balls;
 Area() {} // Can have other parameters
 Area(int ballNb) { createBalls(ballNb); }
 void createBalls(int ballNb)
 {
   balls = new Ball[ballNb];
   for (int i = 0; i < ballNb; i++)
   {
ball[i] = new Ball(this);
   }
 }
}

class Ball
{
 Area area;
 int size;
 Ball(Area a) { area = a; }
 void display() { fill(area.back); ... }
}

BenHem's solution is nice too if the balls doesn't need to be known outside of the Area class, it has the advantage of hiding an "implementation detail". And the balls can access to all fields of the area they belong to, even the private ones!
And Ball class doesn't have to maintain an Area reference, as it is implicit as being the parent class.

Since you have a tentative implementation, perhaps you can show it (cut out stuff unneeded for the problem, if too long) so we can see where the problem is.
Re: OOP Question passing variables
Reply #4 - Sep 30th, 2009, 5:50am
 
Thx to both of you. So i now got 2 solutions and both seems to work, at least at the beginning. So i wanna become more clear on what i actually want to do to figure what would be the best way.

Posting some of my code is a bit complicated cause i have a lot of stuff in there that would take some time to take out. Serialevents, twitterStuff etc.

so what ive got is a World that could contain alot of different species, the species class is "unseen" but contains alot of information, like the DNA, and all the parameters the animals of this species should have.
in this class i want to spawn the a number of animals, all behving autark but belonging to the same species.

What is important to make for later use that i will be able to check for other animals in other species. For example when using collision detection. So lets say, aninmals of the same species, behave different to each other, but also behave different to species2 and species3.
another example. Touch one of your species, become green. touch one of species1 get smaller. species3 get bigger.

Hope its clear, at least a bit.

Would that be possible with BenHems example?

Re: OOP Question passing variables
Reply #5 - Oct 1st, 2009, 5:52am
 
could some of you point out what the best way to do this would be. Would be helpful.
Re: OOP Question passing variables
Reply #6 - Oct 1st, 2009, 8:26am
 
I am sorry, but I am not sure how your case fits in the original question...
Or what is your question/problem.

Basically, there is no "best" way, as long as you have a way that works for you... The difference between the two methods is more a matter of convenience when writing code. Since you don't make a library or other reusable code, the exact method isn't really important. Is it?
Re: OOP Question passing variables
Reply #7 - Oct 1st, 2009, 8:41am
 
my initial question was already answered by both of you as both ways seems to work in case of using the variables in the subclass.
So i will probably use BenHems example as it seems to be simpler. I just wanted to be sure that i wont get any problems later when checking for collision like i mentioned in my last post.
But i guess i will just give it a try. Thx for help!
Re: OOP Question passing variables
Reply #8 - Oct 2nd, 2009, 8:22am
 
When writing it the way BenHem suggested. Can i split it to 2 tabs somehow or does it have to be in one now?
Re: OOP Question passing variables
Reply #9 - Oct 2nd, 2009, 9:03am
 
They must be in the same tab...
Re: OOP Question passing variables
Reply #10 - Oct 2nd, 2009, 9:04am
 
k, i thought so. thx
Page Index Toggle Pages: 1