...

java- class loading help

zecrose
03-07-2005, 01:43 AM
please remember that i am just a teenager in a high-school.

My friend and I are trying to make this text-based tactics game with java. Everything is going pretty smoothly, except one.

We are trying to make an user interface, via console, as interactive as possible. So, we need quiet of commands. How would you manage all these commands? I was thinking about shoving all the classes into Object[] and calling them as users need it, but we hit the wall when I figured out that it is not possible to instance them all automatically.

What is a good way to manage insane amount of class files and calling them as they fit? Or, should i just paste the whole program of 20000 if/else statements?

Antoniohawk
03-07-2005, 09:20 PM
I think that it might help a lot if we could see some code. If you're thinking that you'll need 20,000 if/else statements then it seems to me that you might want to rethink your approach. :p

cfc
03-08-2005, 01:29 AM
Yeah, it's entirely possible to get class names using RTTI (as usual, I'll refer you to Chapter 10 of Thinking in Java, 3rd Edition (http://www.mindview.net/Books/TIJ/) which is free in electronic format), but as AntonioHawk said you'd probably be better off attempting this in a different way. Interfaces and Inheritance exist for a reason (or many reasons) after all, but without any code or even a structural explanation of any kind I can't say for sure.

Simple RTTI methods include the instanceof keyword, Class.isInstance() [which is functionally identical to instanceof], Class.forName(), <your class>.getClass() and <your class>.class.
Class.forName(), .class, and .getClass() will return Class objects.
Reflection could be more than you need but could prove useful depending on how you're building your application.

You can check if a class is of a specific type using the instanceof keyword mentioned above:
if (someObject instanceof MyClass) {
// do something
}

Note that instanceof and isInstance() will return true if someObject is of a class that is derivative of MyClass while comparison of Class objects will not.

zecrose
03-12-2005, 08:46 PM
that helped. I thank you both. hooah!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum