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 › Dynamically overloading methods
Page Index Toggle Pages: 1
Dynamically overloading methods? (Read 703 times)
Dynamically overloading methods?
Jul 21st, 2005, 2:32pm
 
Maybe I'm asking too much on the language in this one... it's also possible I'm thinking about this the wrong way, so here goes:

In my previous thread, Dynamically Resizing Arrays, I introduced a little program I was working on: a 3d animated humanlike model (slightly old version viewable here)

What I want to do is give it a way to have the animation code for each bone stored directly in each of the bones; so what that means is basically I want to override a particular method (lets call in bone.animate()) for each instantiation of the bone class. Is this even possible in Java Or would I need to inherit the class a million times and instantiate each of those Or is there a better solution entirely that I havn't thought of
Re: Dynamically overloading methods?
Reply #1 - Jul 21st, 2005, 5:03pm
 
http://processing.org/discourse/yabb_beta/YaBB.cgi?action=search2

Have you tried abstract class definitions?

Here's a timeline that I wrote a while ago. Maybe you could adapt it to suit your needs.

As you can see each definition of a timeline element has its unique own implementation of the display() method.

If you look at the TimeLineElement class def you'll see the declaration of an abstract method called display(); which would then be required by each bone type in your case

Code:

public abstract class Bone {

 Bone () {
 
 }
 
void genericMethod() {

// common to all bones
}

 abstract void animate();
}



class femur extends bone {

void animate() {

}

}


So you would still have access to all the parent generic methods, but each one would have a unique animate.


Re: Dynamically overloading methods?
Reply #2 - Jul 21st, 2005, 7:08pm
 
That would definitely work and do what I want (until, perhaps, it comes to making multiple animations, but that's yet a step away), but it seems to be a messy way to do it: it requires making 16 different classes (in my case, anyway) when it seems to me that one ought to suffice (having a generic bone class, and simply being able to modify the data that's used to calculate the rotation angles).

Perhaps I come from too dynamic of a programming background and that's spoiled me.

Is it perhaps possible, then, to have an Animate interface with different classes for different animations, and to have my Bone object contain some object that implements Animate? That solution may work best (since I can keep an array of objects for animation and choose which I want, depending if I want the character to run, or walk, or dance, or whatever).

Well, I know how to make an interface and a class that implements it... but I suppose what I'm wondering is how do I make a class contain *some* object that implements that interface?

Code:

interface foo {
void animate (...);
}

class femurFoo implements foo {
...
}

class spineFoo implements foo {
...
}

class bone {
float x, y, z;
// how do I include *anything* that implements foo here?

... etc ...
}


[edit] I seem to have figured it out. Thanks for your help! I'll upload the result upon completion.
Re: Dynamically overloading methods?
Reply #3 - Jul 21st, 2005, 9:04pm
 
And indeed, the method I expected worked.

http://www.rpi.edu/~laporj2/random/walkie2_preview/

What I do is have a separate object for each of X, Y, and Z rotations of bones, and I select a rendering method for each (currently implemented are Inert (no movement), Linear (linear movement), and Sinusoidal (curved)).

It works pretty well and compacts my code, while leaving me the flexibility to do what I want.

Thanks again! I wouldn't have found out about interfaces if you hadn't noted the abstract classes.
Re: Dynamically overloading methods?
Reply #4 - Jul 21st, 2005, 10:25pm
 

Gives me an opportunity to look at interfaces. Thanks
Page Index Toggle Pages: 1