I am trying to figure out an effective way of getting a random class from my project. There are nine different classes(ten including the startup), each one with a different behavior(which extend a declaratoid class that is used in the main function). I need to be able to have the function to run differently on each startup. What do I need to do? Edit:Thanks for the answer, but I am now running into another problem. I have to pass the result as the first parameter in another the function now.
2 Answers
Create a factory method that gets a random number and creates an object based on that, in a switch:
public static YourInterfaceType createRandom() {
Random r = new Random();
switch(r.nextInt(10)) {
case 1: return new FirstType();
case 2: return new SecondType();
// etc
default: return new LastType();
}
}
Edit More exact definition of words. :)
4 Comments
aioobe
Technically speaking you're not creating classes.
Todd
@aioobe I took the intent to mean instances of already defined classes. If OP provides more detail and this is off the mark, I'm happy to remove it.
gknicker
He was referring to your statement
and creates a class based on that - not true. It's creating an instance of an existing class, not creating a class.Aaron Cottrill
Thank you for the answer Todd. It looks exactly like what I need.