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 & HelpSyntax Questions › is it possible to check for a parent type
Page Index Toggle Pages: 1
is it possible to check for a parent type? (Read 509 times)
is it possible to check for a parent type?
Feb 8th, 2009, 7:26pm
 
I have created a class that creates a particle child. Is it possible to refer to this class via the particle? Or even this parent's attributes?

I think this is elementary question and the answer is probably no, but I could use this if it is in fact possible.

thanks for the help in advance.
Re: is it possible to check for a parent type?
Reply #1 - Feb 8th, 2009, 11:49pm
 
If you pass this as parameter to the constructor of the child (or a setter, like: particle.setParent(this);), you can set this parameter (of same type as the parent) to a field of your child, then use it freely (access its visible fields, use its visible methods, etc.).
Re: is it possible to check for a parent type?
Reply #2 - Feb 9th, 2009, 12:34am
 
thanks PhiLho. Again, your help is invaluable...

Before hearing from you I was considering creating a new Edge or Spring class as a parent to the Particle system's springs to be able to categorize each spring in my list of springs within my particle system. I was trying to somehow encode each spring based on the particles to which they are connected - but it's actually the parent class that is holding the data. So in theory each spring could be categorized if they had a parent class with fields for doing this, no?

I had no idea that anything could be inherited like that...so useful
Re: is it possible to check for a parent type?
Reply #3 - Feb 9th, 2009, 1:18am
 
tried it and I think there may have been a misunderstanding. The parent class is not of the same type...What I referred to as a parent is actually just my own class I made which instantiates its own particle. When I tried the particle.setParent(this); I was told the function does not exist..or am I missing something...?
Re: is it possible to check for a parent type?
Reply #4 - Feb 9th, 2009, 2:54am
 

In that case I think you should create a 'setParent(Parent p)'
method in particle class,

and when in the Parent class a Particle is created
Particle p1 = new Particle();
instead, you do this :
Particle p1 = new Particle(this);

...
inside the Particle class, you do:
class Particle {
private Parent p;
//add a new  constructor
public Particle(Parent pParent){
this.p = pParent;
// -- do other stuff you normally do in
// the constructor
}


PhilHo, is this in line with your solution ?
Re: is it possible to check for a parent type?
Reply #5 - Feb 9th, 2009, 3:05am
 
the only problem is that the particle class is part of Traer's physics library - do I have access to it? Can I add this sort of stuff?
Re: is it possible to check for a parent type?
Reply #6 - Feb 9th, 2009, 5:05am
 
wow...so I can extend it in other ways? this might solve a whole bunch of problems.

I will try it tomorrow...thanks so much!
Re: is it possible to check for a parent type?
Reply #7 - Feb 9th, 2009, 5:33am
 
oops. I actually deleted my the post :
summarising :
1. the Traer ParticleSystem and Particle classes are not final but the ParticleSystem:makeParticle(float,float
,float,float) method is.
So you can probably extend the two classes and add the new constructor and the setParent methods.
2. should the Traer library change in the future, you might have problems.

check this one out too :
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=LibraryProblems;action=display;num=1200886110


Page Index Toggle Pages: 1