Results 1 to 6 of 6
Thread: Compiling with Different Classes
- 01-30-2009, 05:09 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
Compiling with Different Classes
I'm not exactly sure how to explain what I am trying to do so I'll just say it and see if it makes any sense.
I have a Main.java that creates two different classes. A Player1 and Player2 class. My problem is that these 2 classes code can change each time I compile. For example, the project I'm working on is a very small game that incorporates two players using their own AIs.
So they'll be uploading their .java AI file to maybe say a webserver somewhere and I can download it and use it in the program.
How is it possible that I hook in their code to that Player1 or Player2 class?
Any tutorial suggestions I can look at?
The only thing I can come up with is that when they upload their code and I grab it off the server is to rename it to Player1.java before compiling. Any other suggestions?
Thanks,
Monc
- 01-30-2009, 06:35 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
Interfaces.
If you are familiar with interfaces (or perhaps even subclasses), you can have your Main.java interact with the interface for each player without worrying about what's going on behind the scenes. Of course, the classes for either player must implement the interface properly.
Read more about it here: What Is an Interface? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)
(sorry, not very good at finding useful resources)
- 01-31-2009, 03:06 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-31-2009, 05:08 AM #4
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
I just call an instance of each one:
Player1 newPlayer1 = new Player1();
Player2 newPlayer2 = new Player2();
- 01-31-2009, 05:13 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That's not creating classes lol. That's call instantiate of classes. Simply means creating objects of classes. ;)
- 02-03-2009, 06:20 AM #6
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Well, to load in a class after the fact (after the program has started), you need to explicitly load it via a ClassLoader.
Here's the exmaple the java api shows:
Once you've loaded the class in this way, you use reflection to instantiate an instance.Java Code:class NetworkClassLoader extends ClassLoader { String host; int port; public Class findClass(String name) { byte[] b = loadClassData(name); return defineClass(name, b, 0, b.length); } private byte[] loadClassData(String name) { // load the class data from the connection . . . } }
In the example above, you would call the 'findClass' method to get a Class object, and then call the 'newInstance' method on that class to create the instance of 'Player1' or 'Player2'.
You will want Player1, Player2, etc. to implement a common interface, so you can just cast the result of newInstance to that type and interract with it.
Similar Threads
-
Compiling and using jar file for custom classes
By MAILMIRZA in forum New To JavaReplies: 3Last Post: 01-12-2009, 04:56 PM -
note while compiling
By j2vdk in forum New To JavaReplies: 3Last Post: 09-03-2008, 11:52 PM -
I have 3 errors after compiling
By coco in forum JDBCReplies: 2Last Post: 10-18-2007, 09:32 AM -
Error during compiling
By boy22 in forum New To JavaReplies: 2Last Post: 08-03-2007, 02:42 AM -
problems when compiling
By valery in forum New To JavaReplies: 2Last Post: 07-25-2007, 07:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks