![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 149
Rep Power: 3
![]() |
variable type
Is there any variable type to possibly hold commands? I know it sounds like an odd request... Something like this:
WhateverThatTypeWouldBeCalled someCommands=new WhateverThatTypeWouldBeCalled{
here's a command;
here's another;
and yet another!;
};I know it looks a lot like just defining a method, but I want to be able to handle that variable like any other variable--like be able to put it into lists or copy it into another variable. Once again, I know this sounds like an odd request, but any help would be appreciated. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
I don't think so, and if it was possible I would never use it. To do something like it, put the class in the same class that function is in. You will only need a few extra characters
. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 4
![]() |
I'm curious as to why you would want to? I'm not hacking on you, but I can't see any need to do that. There was a timw when Iwanted to be able to dynamically name instances (which sounds sort of similar to what you want to so. You basically want to dynamically name methods, whether or not you realize it), until it was pointed out to me that my design had a flaw, and I was stupid.
So maybe if you explain what you are trying to do, or why, we can help you figure out a more conventional method of accomplishing it. ![]() EDIT: Just to clarify.. I am not calling you stupid. I was merely reporting what I was told. ![]()
__________________
HijackThis Team-SFDC Last edited by groovicus; Jan 8th, 2006 at 10:07 AM. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jan 2006
Location: Dallas, TX
Posts: 49
Rep Power: 0
![]() |
Could you tell us a little more about what you are doing and why? It might help us make alternate suggestions. From what you've written it seems like you want something to hold a list of command strings. Is that correct? Are you testing some input to see if it matches any command strings or something else?
|
|
|
|
|
|
#5 | ||
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 149
Rep Power: 3
![]() |
Quote:
Quote:
What I'm trying to do is make a mostly passive screensaver-like program that can switch randomly between different methods. That's the reason I want to be able to put the methods into lists--So I can choose randomly from them. I know I could use a switch/case loop, but... I don't know. I guess it just seems to me that there would be a variable to handle commands. |
||
|
|
|
|
|
#6 | ||
|
Programmer
Join Date: Jan 2006
Location: Dallas, TX
Posts: 49
Rep Power: 0
![]() |
Quote:
From the site: Quote:
![]() Edit: Some might recommend making all of your different methods static (which is basically global) and then calling them. I'm not going to lie - that would work. But it's poor programming practice, in my opinion.
__________________
Java Blog |
||
|
|
|
|
|
#7 |
|
Hobbyist Programmer
|
public void MyFun(String ab, String cd, String ef)
{
String z = ab;
String x = cd;
String y = ef;
}
__________________
Hoes telling me to calm down but I'm like fuck that shit!
|
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 149
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#9 |
|
Programmer
Join Date: Jan 2006
Location: Dallas, TX
Posts: 49
Rep Power: 0
![]() |
The string representation of a method is another way of saying it. Here's an example:
Here's the method public void doStuff
{
//do stuff
}Here's the String representation String methodString =
"public void doStuff() \n" +
"{\n" +
" // do stuff \n" +
"}";It's really beside my point though.
__________________
Java Blog |
|
|
|
|
|
#10 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
public interface Command {
public void invoke();
}
public void main(String[] args) {
List someCommands = new Vector<Command>();
// Add a command that prints out "Hello World"
someCommands.add(new Command()
{
public void invoke() {
System.out.println("Hello World");
}
});
// Execute all commands
for (Command command : someCommands) {
command.invoke();
}
} |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|