通过存储在哈希表中的字符串访问函数
如果我将函数名称作为字符串存储在哈希表中。
有没有办法通过存储的字符串访问函数?
编辑 恐怕我正在开发的平台 CLDC1.1/MIDP2.0 不支持反射。
有什么可能的解决方法吗?
If I have function names stored as strings in a Hashtable.
Is there a way to access the functions via the stored strings?
EDIT
I'm afraid the platform that i'm working on CLDC1.1/MIDP2.0 does not support Reflection.
Any workaround possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需使用一长串 else-if 即可:(
虽然我通常不喜欢在源代码中尝试垂直对齐,但我认为在这种情况下这是非常值得的。)
在地图中存储函子是一种替代方案,但可能对于许多 MIDP 应用程序来说,对象大小增加太多。
Just use a big long list of else-ifs:
(Although I generally don't like attempting vertical alignments in source, I think in cases like this it is well worth it.)
Storing a functor in the map is an alternative, bu might increase object size too much for many MIDP applications.
Class.forName() 和 newInstance() 应该存在于 MIDP 1.1 中并且可能有用。由于您没有完全反射,因此您可以创建一个类来包装所有函数调用并调用它们。这假设您提前知道所有函数调用。
除非您正在进行静态调用,否则您将需要对所有对象的引用。有点乱,但可以完成工作。
Class.forName() and newInstance() should be there on MIDP 1.1 and might be useful. Since you don't have full reflection, you could create a class that wraps all the function calls and invokes them. This assumes you know all the function calls in advance.
You'll need references to all the objects unless you're doing static calls. Kinda messy, but would get the job done.
给定函数名称,您可以使用反射来访问该方法。除了函数的名称之外,您还需要知道它的类,并且拥有(或者如果它有无参数构造函数,则通过反射创建,或者您知道要传递给构造函数的参数)一个实例(如果该方法不是静态的),并且您必须知道传递给它所需的参数。
另一种选择是使用 Method 类作为函数的指针。它的优点是了解其类别并了解其参数要求。它的缺点是不可序列化。
编辑:没有办法通过仅将其名称作为字符串来访问 Java 中的方法而无需反射。如果您想要一个指向方法的替代指针,您可以使用匿名内部类来调用您想要的实现已知接口的方法并将其传递给您的映射。这不适合作为映射的键,但它可以作为映射中的值。
Given a function name, you could use reflection to access the method. Besides the name of the function, you need to know its class, and have (or create via reflection if it has a noarg constructor or you know what parameters to pass to the constructor) an instance (if the method is not static) and you have to know the parameters required to pass to it.
Another option is to use the Method class as a pointer to the function. It has the advantage of knowing its class, and knowing its parameter requirements. It has the disadvantage of not being serializable.
EDIT: There is no way to access a method in Java without reflection by just having its name as a string. If you want an alternative pointer to a method, you could use an anonymous inner class that invokes the method you want that implements a known interface and pass that to your map. That would not be appropriate as the key of the map, but it would work as a value in the map.
我没有在javame上尝试过反射,但是在javase上你可以使用反射来动态调用方法。
以下片段摘自:
http://java.sun.com/developer/technicalArticles/ALT/Reflection/< /a>
公共类方法2 {
公共 int add(int a, int b)
{
返回a+b;
}
}
编辑:
您可能想要实际传回一个数字,并使用开关,而不是使用 if..then..else,因为这样更容易阅读,IMO。
但无论如何,考虑到 JavaME 的局限性,这可能是您唯一的选择。
I haven't tried reflection on javame, but on javase you can use reflection to dynamically call a method.
The following snippet is taken from:
http://java.sun.com/developer/technicalArticles/ALT/Reflection/
public class method2 {
public int add(int a, int b)
{
return a + b;
}
}
EDIT:
Rather than using if..then..else you may want to actually pass back a number, and use a switch, as it will be easier to read, IMO.
But, regardless, that is probably your only option given the limitations of JavaME.
我在 Hecl(一个在 J2ME 上运行的解释器)中处理了类似的问题。这在某种程度上取决于您想要处理多少个字符串,但一种有效的方法是使用哈希来查找整数,然后在 switch 语句中使用它,而不是使用 if/then 语句的大列表。顺便说一下,欢迎您使用Hecl源代码;它可以在 Apache 自由许可证下使用。
I deal with similar issues in Hecl, an interpreter that runs on J2ME. It sort of depends on how many strings you want to deal with, but one method that works is to use the hash to look up an integer, and then use that in a switch statement, instead of the big list of if/then statements. By the way, you're welcome to use the Hecl source code; it's available under the liberal Apache license.