为给定值定义回调函数
在Java中,是否可以将某个对象(即字符串)与要调用的函数关联起来?
我有两个类似的菜单,并且都有一个 onClickListener 和一些类似这样的代码:
if (item.id==0) {
doSomeFunction(argument);
}
else if (item.id==1) {
doSomeOtherFunction();
}
else if (item.id==2) {
doStuff(arg2);
}
else {
}
我只想有一个对象(即哈希表),它允许我定义“item.id”->“item.id”之间的关联。 “要执行的代码”。
示例:
0 -> doSomeFunction(argument);
1 -> doSomeOtherFunction();
2 -> doStuff(arg2);
因此,我可以跳过这些条件,只执行为给定 item.id 存储的函数的函数调用。
例如,使用 Lisp,我可以将 item.id 与一个函数相关联,然后使用 Lisp 的“funcall”来调用该函数。
在Java中是否有一个标准的方法来做这样的事情?
是否有其他解决方案可以跳过条件?
请注意,被调用的函数可能需要接收参数。
谢谢。
In Java, is it possible to associate some object (i.e. a String) with a function to be called ?
I have two similar menus and both have a onClickListener with some code like this:
if (item.id==0) {
doSomeFunction(argument);
}
else if (item.id==1) {
doSomeOtherFunction();
}
else if (item.id==2) {
doStuff(arg2);
}
else {
}
I would just like to have an object (i.e. Hashtable) that would allow me to define associations between "item.id" -> "code to execute".
Example:
0 -> doSomeFunction(argument);
1 -> doSomeOtherFunction();
2 -> doStuff(arg2);
As a result I could skip these conditionals and just perform a function call of the function stored for a given item.id.
For example, with Lisp I could associate the item.id with a function and then use Lisp's "funcall" to call that function.
Is there a standard way to do something like this in Java?
Are there alternative solutions to skip the conditionals?
Please note that the called functions may need to receive parameters.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您的函数看起来不需要返回任何值,因此您可以将每个 ID 键与调用您想要的方法的
Runnable
对象相关联。例如:如果您需要方法能够返回值,则可以使用
Callable
而不是Runnable
。您还可以创建自己的类似界面并在需要时使用它。目前,在 Java 中,此类事情必须通过具有单个抽象方法(例如 Runnable)的类实例来完成。 Java 7 或 8(取决于他们决定如何继续)将添加 lambda 表达式和方法引用,我认为这是您真正想要的。方法引用将允许您执行类似的操作(使用
Map
就像上面一样)。编辑:
您的更新表明其中一些方法需要传递参数。但是,您似乎要求两件不同的事情。在声明映射的示例中,
指定了参数,表明它们是声明映射时可用的值,或者是类中的字段或类似字段。无论哪种情况,您都可以在创建
Runnable
时声明它们:这里,
argument
要么需要是一个字段,要么被声明为final< /代码>。
另一方面,如果您在创建映射时没有对参数的引用,则必须创建一些所有方法调用都可以共享的一致接口。例如,适用于您的示例的最不常见的接口是声明两个参数和
void
返回的接口:那么您可以:
每个
CustomRunnable
仅使用参数它需要的,如果有的话。然后你可以像这样进行调用:显然,这变得非常难看,但这几乎是你在这种情况下必须做的。
Since it looks like your functions don't need to return any values, you could associate each ID key with a
Runnable
object that calls the method you want. For example:If you need the methods to be able to return a value, you can use
Callable
rather thanRunnable
. You can also create your own similar interface and use it instead if needed.Currently in Java this sort of thing has to be done with an instance of a class with a single abstract method such as
Runnable
. Java 7 or 8 (depending on how they decide to proceed) will add lambda expressions and method references, which I think is what you really want. Method references would allow you to do something like this (using aMap<Integer, Runnable>
just like above).Edit:
Your update indicates that some of these methods would need to have arguments passed to them. However, you seem to be asking for two different things. In your example of declaring the mapping
the arguments are specified, indicating that they are either values that are available at the time you declare the mapping or they're fields in the class or some such. In either case, you'd be able to declare them when you create the
Runnable
:Here,
argument
would either need to be a field or be declaredfinal
.If, on the other hand, you don't have references to the arguments at the time you create the mapping, you'd have to create some consistent interface that all the method calls could share. For example, the least common interface that could work for your sample would be one that declared two parameters and a
void
return:Then you could have:
Each
CustomRunnable
only uses the arguments that it needs, if any. Then you could do the call like this:Obviously, this is getting pretty ugly but it's pretty much what you'd have to do in that case.
是的,有多种方法可以做到这一点。一个例子是匿名实现一个接口并将其存储在 HashMap 中,如下所示:
Yes, there are several ways to do this. One example is to implement an interface anonymously and store it in a HashMap, something like this:
通常要做的事情是向每个菜单项添加一个不同的操作侦听器(命令),作为一个简短的匿名内部类实现。不幸的是,目前的语法很冗长,对启动时间没有任何帮助,但它通常仍然是最好的方法。
你没有说你正在使用哪个库。
onClickListener
不是 Swing,但如果您使用 Swing,则需要JMenuItem.addActionListener
。The usual thing to do is to add a different action listener (command) on to each menu item, implemented as a short anonymous inner class. Unfortunately the syntax is currently verbose and it doesn't help start up time any, but it's still generally the best approach.
You don't say what library you are using.
onClickListener
isn't Swing, but if you were using Swing, you'd wantJMenuItem.addActionListener
.Java Swing 的解决方法是 Action 类。基本上它遵循命令模式......
Java Swing way of answer for this is Action class. basically it follows command pattern...