如何在Java中转换函数中的字符串?
我试图在Java中创建一个以字符串和double作为参数的函数,如下所示:如何在Java中转换函数中的字符串?,java,string,function,Java,String,Function,我试图在Java中创建一个以字符串和double作为参数的函数,如下所示: public static double calc(String fx, double arg) { return fx.convertToFunction(arg); // Pseudocode } //assume that you have an Object that you want to invoke the method on MyObject obj = new MyObject(); /
public static double calc(String fx, double arg) {
return fx.convertToFunction(arg); // Pseudocode
}
//assume that you have an Object that you want to invoke the method on
MyObject obj = new MyObject();
//the variable `method` will hold your function
java.lang.reflect.Method method;
//paremters can be provided to identify a specific method among overloaded methods
method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
//invoke the method of your object
method.invoke(obj, arg1, arg2,...);
例如,要计算一个数字的余弦(例如PI),代码为:
calc("cos", Math.PI);
函数calc必须将“cos”转换为Math.cos()
这至少是可能的吗?您正在寻找的是Java中的反射 请看一下这个主题: 如果没有对异常进行必要的检查,您的代码可能如下所示:
public static double calc(String fx, double arg) {
return fx.convertToFunction(arg); // Pseudocode
}
//assume that you have an Object that you want to invoke the method on
MyObject obj = new MyObject();
//the variable `method` will hold your function
java.lang.reflect.Method method;
//paremters can be provided to identify a specific method among overloaded methods
method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
//invoke the method of your object
method.invoke(obj, arg1, arg2,...);
您正在寻找的是Java中的反射 请看一下这个主题: 如果没有对异常进行必要的检查,您的代码可能如下所示:
public static double calc(String fx, double arg) {
return fx.convertToFunction(arg); // Pseudocode
}
//assume that you have an Object that you want to invoke the method on
MyObject obj = new MyObject();
//the variable `method` will hold your function
java.lang.reflect.Method method;
//paremters can be provided to identify a specific method among overloaded methods
method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
//invoke the method of your object
method.invoke(obj, arg1, arg2,...);
你想做的最简单的方法是 您可以使用if-else或switch轻松完成此操作
calc("cos", Math.PI);
在函数中,将其检查为
if(stringValue.equals("cos")){
call the cos function
}
你想做的最简单的方法是 您可以使用if-else或switch轻松完成此操作
calc("cos", Math.PI);
在函数中,将其检查为
if(stringValue.equals("cos")){
call the cos function
}
不可以,您可以使用带有开关的设计模式或实现:
switch (command) {
case "cos" : return Math.cos(dblParam);
}
不可以,您可以使用带有开关的设计模式或实现:
switch (command) {
case "cos" : return Math.cos(dblParam);
}
真的不容易。您可以定义映射以将名称与方法(或者更好地说,是函数接口)关联起来,或者使用反射 例如(使用函数接口,Java 7语法):
//定义函数
final Runnable cosFunction=new Runnable(){
@凌驾
公开募捐{
//做余弦
}
}
//存储功能
Map functionMap=newhashmap();
functionMap.put(“cos”,cosFunction);
//检索函数
final Runnable function=functionMap.get(“cos”);
函数run();
请注意,使用Runnable
不允许返回值
在函数式编程中,这更容易,我建议您研究Clojure或Scala,它们是基于JVM的。真的不容易。您可以定义映射以将名称与方法(或者更好地说,是函数接口)关联起来,或者使用反射 例如(使用函数接口,Java 7语法):
//定义函数
final Runnable cosFunction=new Runnable(){
@凌驾
公开募捐{
//做余弦
}
}
//存储功能
Map functionMap=newhashmap();
functionMap.put(“cos”,cosFunction);
//检索函数
final Runnable function=functionMap.get(“cos”);
函数run();
请注意,使用Runnable
不允许返回值
在函数式编程中,这更容易,我建议您研究Clojure或Scala,它们是基于JVM的。我建议您重新考虑这是否是一个好主意,但是您可以这样做
public static double calc(String fx, double arg) {
try {
Method math = Math.class.getMethod(fx, double.class);
return (Double) math.invoke(null, arg);
} catch (Exception e) {
throw new IllegalArguementException(e);
}
}
我建议你重新考虑这是否是一个好主意,但你可以这样做
public static double calc(String fx, double arg) {
try {
Method math = Math.class.getMethod(fx, double.class);
return (Double) math.invoke(null, arg);
} catch (Exception e) {
throw new IllegalArguementException(e);
}
}
您需要使用反射api,如下所示:
String methodName = "cos";
double arg = Math.PI;
try {
Method method = Math.class.getDeclaredMethod(methodName,
double.class);//get cos method from math class that accepts double as parameter
method.setAccessible(true);//i trust this call
double time = (Double) method.invoke(null, arg);//invoke the method with PI
System.out.println(time);
} catch (Exception e) {//handle all exceptions or individual according to business need
e.printStackTrace();
}
您需要使用反射api,如下所示:
String methodName = "cos";
double arg = Math.PI;
try {
Method method = Math.class.getDeclaredMethod(methodName,
double.class);//get cos method from math class that accepts double as parameter
method.setAccessible(true);//i trust this call
double time = (Double) method.invoke(null, arg);//invoke the method with PI
System.out.println(time);
} catch (Exception e) {//handle all exceptions or individual according to business need
e.printStackTrace();
}
Java提供了一个名为。这提供了按名称访问方法、变量等的方法。这些工具中的大多数都位于java.lang.Class对象上
- (名称,args)以获取java.reflect.Method引用
- 使用method.invoke(null,args)调用方法引用。这里的null是实例,这对于静态方法没有意义
Java提供了一个名为。这提供了按名称访问方法、变量等的方法。这些工具中的大多数都位于java.lang.Class对象上
- (名称,args)以获取java.reflect.Method引用
- 使用method.invoke(null,args)调用方法引用。这里的null是实例,这对于静态方法没有意义
你们应该把接受输入fx的if/else状态放在数学课堂上使用。使用字符串作为函数名太复杂了。您正在寻找反射或更好的代码结构。您是指
function
还是method
?为什么首先要使用字符串?例如,这是用户通过控制台输入的吗?@MandarPandit在Java中没有函数。您应该输入if/else状态,并在数学类中使用它。使用字符串作为函数名太复杂了。您正在寻找反射或更好的代码结构。您是指function
还是method
?为什么首先要使用字符串?这是用户通过控制台输入的吗?@MandarPandit在Java中没有任何函数。这应该是一个注释,因为它没有提供问题的答案谢谢你们,我试图在我的Eddite答案中呈现引用文章的本质。这应该是一个注释,因为它不能回答这个问题谢谢大家,我试着在我的Eddite答案中呈现引用文章的精髓。对于所有可能的场景,你都要这样写吗?我认为鉴于OP中的有限示例,这是一个好建议。@AbimaranKugathasan取决于需求。如果有2或3个函数,那么就可以了。否则,可以使用多种方法用来动态地解决问题。你打算在所有可能的情况下都这样写吗?我认为鉴于OP中的有限示例,这是一个好建议。@AbimaranKugathasan取决于需求。如果有2或3个函数,那么就可以了。否则,可以使用多种方法动态地解决问题。。