在 Java 和 C 中在运行时调用名为“string”的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2882948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a method named "string" at runtime in Java and C
提问by dbtek
How can we call a method which name is stringat runtime. Can anyone show me how to do that in Java and C.
我们如何在运行时调用名称为字符串的方法。谁能告诉我如何在 Java 和 C 中做到这一点。
采纳答案by aioobe
In java it can be done through the reflection api.
在java中它可以通过反射api来完成。
Have a look at Class.getMethod(String methodName, Class... parameterTypes)
.
看看Class.getMethod(String methodName, Class... parameterTypes)
。
A complete example (of a non-static method with an argument) would be:
一个完整的示例(带有参数的非静态方法)将是:
import java.lang.reflect.*;
public class Test {
public String methodName(int i) {
return "Hello World: " + i;
}
public static void main(String... args) throws Exception {
Test t = new Test();
Method m = Test.class.getMethod("methodName", int.class);
String returnVal = (String) m.invoke(t, 5);
System.out.println(returnVal);
}
}
Which outputs:
哪些输出:
Hello World: 5
Hello World: 5
回答by DJClayworth
In Java:
在 Java 中:
If class A has a method "string()" then you call it by:
如果类 A 有一个方法“string()”,那么你可以通过以下方式调用它:
A a = new A();
a.string();
C does not have methods and you can't call them. You may be thinking of C++ which essentially has exactly the same syntax.
C 没有方法,你不能调用它们。您可能会想到 C++ 本质上具有完全相同的语法。
回答by Andrzej Doyle
In Java you would use reflection:
在 Java 中,您将使用反射:
Class<?> classContainingTheMethod = ...; // populate this!
Method stringMethod = classContainingTheMethod.getMethod("string");
Object returnValue = stringMethod.invoke(null);
This is a very simple case that assumes your method is static and takes no parameters. For non-static methods, you'd pass in the instance to invoke the method on, and in any case you can pass in any required parameters to the invoke()
method call.
这是一个非常简单的案例,假设您的方法是静态的并且不接受任何参数。对于非静态方法,您需要传入实例来调用方法,并且在任何情况下,您都可以将任何必需的参数传递给invoke()
方法调用。
回答by Edward Dale
In Java, you'll have to use the Java Reflection APIto get a reference to the Methodobject representing your method, which you can then execute.
在 Java 中,您必须使用Java 反射 API来获取对代表您的方法的Method对象的引用,然后您可以执行该对象。
回答by Nikko
In C (or C++) real reflection is not possible as it is a compiled language.
在 C(或 C++)中,真正的反射是不可能的,因为它是一种编译语言。
The most used is to have an associative container (a map) that can link a function name (as a string) to a function pointer. You have to fill in the map in the program with the value you want. This can't be done automatically.
最常用的是有一个关联容器(映射),可以将函数名称(作为字符串)链接到函数指针。您必须使用您想要的值填写程序中的地图。这不能自动完成。
You could also simply have a function that takes a string as parameter and then chose the right function to call with hand-made ifs.
您也可以简单地使用一个将字符串作为参数的函数,然后选择正确的函数来使用手工制作的 ifs 进行调用。
回答by Aif
Here is a base C example, I hope it will help you.
这是一个基本的 C 示例,希望对您有所帮助。
typedef void (*fun)(void);
static void hello()
{
puts("hello world");
}
static void string()
{
puts("string");
}
static void unknown()
{
puts("unknown command");
}
struct cmd
{
char* name;
void (*fun) (struct cmd* c);
};
static struct cmd commands[] = {
{ "hello", hello },
{ "string", string },
{ 0, unknown }
};
static void execute(const char* cmdname)
{
struct cmd *c = commands;
while (c->name && strcmp (cmdname, c->name))
c++;
(*c->fun) (c);
}
int main()
{
execute("hello");
execute("string");
execute("qwerty");
}
回答by dpc.pw
I'm quite sure you can put all your functions into the shared library and load them with dlopen + dlsym.
我很确定您可以将所有函数放入共享库并使用 dlopen + dlsym 加载它们。