简体   繁体   English

Class.getMethod不起作用

[英]Class.getMethod not working

So I had the following code below and called Operators Op = new Operators() elsewhere. 因此,我在下面有以下代码,并在其他地方调用了Operators Op = new Operators() However, I got an error in the getMethod call. 但是,我在getMethod调用中出错。 I admit I'm not completely sure how to use it and got this result by reading other people's code, so any help would be great. 我承认我不太确定如何使用它,并且通过阅读其他人的代码获得了此结果,因此任何帮助都将非常有用。 Thanks. 谢谢。

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class Operators {
    static Map<String, Method> METHODS = new HashMap<String, Method>();
    String ADD = "+"; String MULTIPLY = "*"; String SUBTRACT = "-"; String DIVIDE = "/";
    private static Class[] inputTypes = {Float.class, Float.class}; 

    Operators() throws NoSuchMethodException, SecurityException {
        METHODS.put(ADD, getMethod("add"));
        METHODS.put(MULTIPLY, getMethod("multiply"));
        METHODS.put(SUBTRACT, getMethod("subtract"));
        METHODS.put(DIVIDE, getMethod("divide"));
    }

    static Method getMethod(String s) throws NoSuchMethodException {
        return Operators.class.getMethod(s, inputTypes);
    }

    public static float add(float x, float y) {
        return x+y;
    }

    public static float multiply(float x, float y) {
        return x*y;
    }

    public static float subtract(float x, float y) {
        return x-y;
    }

    public static float divide(float x, float y) {
        return x/y;
    }
}

Edit. 编辑。 The line referenced was return Operators.class.getMethod(s, inputTypes); 引用的行是return Operators.class.getMethod(s, inputTypes); inside the getMethod method. getMethod方法中。

It might give me a better idea of how to help you once I understand what on earth you are trying to do, but on first glance, this might be it: 一旦我了解了您到底想做什么,就可以为我提供一个更好的帮助的更好的主意,但是乍一看,可能就是这样:

the inputTypes-array houses two Float.class-es , but your methods use the primitive types. inputTypes-array包含两个Float.class-es ,但是您的方法使用基本类型。 Float with a capital letter is different from float lowercase, hence I would expect a NoSuchMethodException . 带大写字母的Float与小写的float不同,因此,我期望NoSuchMethodException

you can also avoid to declare the input parameters type by changing the class as following and you can do it outside the constructor: 您还可以通过如下更改类来避免声明输入参数的类型,并且可以在构造函数之外进行操作:

public class Operators { 公共类运营商{

static final String ADD = "+";
static final String MULTIPLY = "*";
static final String SUBTRACT = "-";
static final String DIVIDE = "/";

private static Method[] methods;
static Map<String, Method> methodsMap = new HashMap<String, Method>();
static {

    methods = Operators.class.getMethods();
    try {
        methodsMap.put(ADD, getMethod("add"));
        methodsMap.put(MULTIPLY, getMethod("multiply"));
        methodsMap.put(SUBTRACT, getMethod("subtract"));
        methodsMap.put(DIVIDE, getMethod("divide"));

    } catch (NoSuchMethodException e) {
        // handle error
        e.printStackTrace();
    }
}



static Method getMethod(String s) throws NoSuchMethodException {

    for (Method method : methods) {
        if (method.getName().equalsIgnoreCase(s))
            return method;
    }

    throw new NoSuchMethodException(s);
}

public static float add(float x, float y) {
    return x + y;
}

public static float multiply(float x, float y) {
    return x * y;
}

public static float subtract(float x, float y) {
    return x - y;
}

public static float divide(float x, float y) {
    return x / y;
}

} }

and you can use the methods map: 您可以使用方法映射:

System.out.println(Operators.methodsMap.get("+").invoke(null, 1.0, 1.0));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM