![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 149
Rep Power: 4
![]() |
classes and methods
Is there any way to copy a class or method to another variable (Not create an instance of that class)? The reason I ask is because I want to create a list of methods from which I can choose randomly.
|
|
|
|
|
|
#2 |
|
Professional Programmer
|
I'm not sure if i get it, but you could create a class with static methods. So you could use it like: MyClass.MyMethod;
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
I am not 100% clear on your question, but you said you wanted to be able to choose a method at random. Well you can either use an object or static methods. And simply have a random number generator and use a switch statement on it. I am not a Java programmer so I can't tell you how to do a random number generator but there are a billion tutorials for it, just google for it, and it will surely come up. Here is an example once you have your random number...
switch(randomNumber)
{
case 1: Class.MethodOne();
case 2: Class.MethodTwo();
case 3: Class.MethodThree();
} |
|
|
|
|
|
#4 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
You can use anonymous classes to wrap your functions in. e.g: methodWrapper = new Runnable() {
public void run() {
yourMethod();
}
};
methodWrapper.run();interface MethodWrapper {
boolean invoke(String s);
} |
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jan 2006
Location: Dallas, TX
Posts: 49
Rep Power: 0
![]() |
This would be a good exercise for a functional programming language.
From a design point-of-view, the strategy design pattern might fit the bill. |
|
|
|
|
|
#6 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
|
|
|
|
|
|
|
#7 | |
|
Programmer
Join Date: Jan 2006
Location: Dallas, TX
Posts: 49
Rep Power: 0
![]() |
Quote:
![]() |
|
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
|
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
Even C has function pointers...
|
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
How many years? I thought C# was pretty young compared to Java.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|