Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 4th, 2006, 1:29 AM   #1
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
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.
Writlaus is offline   Reply With Quote
Old Jan 4th, 2006, 2:33 AM   #2
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 381
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
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 !
xavier is online now   Reply With Quote
Old Jan 4th, 2006, 3:37 AM   #3
Darkhack
Hobbyist Programmer
 
Darkhack's Avatar
 
Join Date: Dec 2005
Location: Kansas City
Posts: 102
Rep Power: 3 Darkhack is on a distinguished road
Send a message via AIM to Darkhack
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();
}
Darkhack is offline   Reply With Quote
Old Jan 4th, 2006, 5:17 AM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Writlaus
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.
If you know which methods you wish to randomly choose from before runtime, then you can use Darkhack's approach. If you wish to randomly choose from a list of methods that are only decided at runtime, then you need to take a different approach.

You can use anonymous classes to wrap your functions in. e.g:
methodWrapper = new Runnable() {
    public void run() {
        yourMethod();
    }
};
methodWrapper.run();
The method wrapper object created from the anonymous class can then be stored in a list. I've used the Runnable interface as a base, but you can create your own (in fact, it's probably a better idea, to). For instance, the interface below has an 'invoke' method that takes in a string and outputs a boolean:
interface MethodWrapper {
    boolean invoke(String s);
}
Arevos is offline   Reply With Quote
Old Jan 4th, 2006, 6:42 AM   #5
alcdotcom
Programmer
 
Join Date: Jan 2006
Location: Dallas, TX
Posts: 49
Rep Power: 0 alcdotcom is on a distinguished road
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.
alcdotcom is offline   Reply With Quote
Old Jan 4th, 2006, 7:23 AM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by alcdotcom
This would be a good exercise for a functional programming language.
Or indeed any language where functions are first class objects. Pity Java isn't one of those languages, otherwise the solution would be a lot easier.
Arevos is offline   Reply With Quote
Old Jan 4th, 2006, 9:08 AM   #7
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
Even C has function pointers...
Klipt is offline   Reply With Quote
Old Jan 4th, 2006, 2:53 PM   #8
alcdotcom
Programmer
 
Join Date: Jan 2006
Location: Dallas, TX
Posts: 49
Rep Power: 0 alcdotcom is on a distinguished road
Quote:
Originally Posted by Arevos
Or indeed any language where functions are first class objects. Pity Java isn't one of those languages, otherwise the solution would be a lot easier.
Java and C# have shadowed each other for years. Now that the new version of C# has OCaml-style lambda functions, maybe Java will follow suit.
alcdotcom is offline   Reply With Quote
Old Jan 4th, 2006, 4:01 PM   #9
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by alcdotcom
Java and C# have shadowed each other for years. Now that the new version of C# has OCaml-style lambda functions, maybe Java will follow suit.
Yep. Here's hoping! C#'s getters and setters look very nice, also.
Arevos is offline   Reply With Quote
Old Jan 5th, 2006, 3:19 AM   #10
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
How many years? I thought C# was pretty young compared to Java.
Klipt is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC