java - 如何调用字符串数组中的 Java 方法? (java.lang.NoSuchMethodException)

标签 java arrays string invoke

我有一个字符串数组,其中包含方法名称,我试图在循环中调用它。我已经检索了类中所有变量的名称,并添加了所需的文本来填充整个函数,并将整个字符串添加到列表中:

String[] methodNames= new String[16];
Class c = telegramm.class;
Field[] fields = c.getDeclaredFields();

int x=0;
for (Field field : fields) 
{               
    String str = field.getName();
    String variableName = str.substring(0, 1).toUpperCase() + str.substring(1);
    // This is how I initially intended to call the method
    // methodNames[x] = "set" + variableName + "(parts[" + x + "]);";

    // but with the method I found I used this
    methodNames[x] = "set" + variableName";

    Method gs1Method = tClass.getMethod("getString1", new Class[] {});
    //String str1 = (String) gs1Method.invoke(t, new Object[] {});
    //System.out.println("getString1 returned: " + str1);
    x++;
}

因此,我将方法调用 String 添加到数组 methodNames 中。

我找到了一种调用方法的方法:

Method gs1Method = tClass.getMethod("getString1", new Class[] {});
String str1 = (String) gs1Method.invoke(t, new Object[] {});
System.out.println("getString1 returned: " + str1);

这有效,但仅当您硬编码方法名称(“getString1”)时。

现在我想实现一种通过 methodNames 数组调用这些函数的方法。

当我传递 methodNames 数组的对象作为参数时:

getMethod(methodNames[x], new Class[] {});

我收到错误:

Exception in thread "main" java.lang.NoSuchMethodException: 
kin.gateway.adient.Testing.setBoolean1(java.lang.String)
at java.lang.Class.getMethod(Class.java:1786)
at kin.gateway.adient.ClassMethodTest.main(ClassMethodTest.java:31)

我还尝试将字符串添加到单个变量StringvariableName =“set”+variableName;但得到了相同的结果。

为什么它不接受字符串作为变量?

最佳答案

尝试这个示例应该可以帮助您找到错误,之后您可以根据您拥有的方法在其上添加循环:

1 - 您的 Telegramm 类(class):

public class Telegramm {

    String value1;
    String value2;

    public String getValue1() {
        return this.value1;
    }

    public void setValue1(String value) {
        this.value1 = value;
    }

    public String getValue2() {
        return this.value2;
    }

    public String setValue2(String value) {
        this.value2 = value;
        return this.value2;
    }

}

2 - 调用方法:getValue1() 和 setValue2()

try {
            // Create our Telegramm instance
            Telegramm telegramm = new Telegramm();
            telegramm.setValue1("value1");
            telegramm.setValue2("value2");

            // Invoke public static String getValue1()
            Method getValue1Method = telegramm.getClass().getMethod("getValue1", null);
            String result = (String)getValue1Method.invoke(telegramm);
            System.out.println("result invocation getValue1() : " + result);

            // Invoke public static String setValue2()
            getValue1Method = telegramm.getClass().getMethod("setValue2", new Class[] {String.class});
            result = (String)getValue1Method.invoke(telegramm, "ValueX");
            System.out.println("result invocation setValue2() : " + result);

        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

关于java - 如何调用字符串数组中的 Java 方法? (java.lang.NoSuchMethodException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48703959/

相关文章:

java - 在weblogic中从AD获取自定义用户属性

java - 如何解决此 "required a bean"错误

javascript - 如何使用 javascript 查找并记录数组中最长的重复数字序列

javascript - 如何使用 match() 分割数组中的行?

java - 如何使用 Joda Time 计算事件发生次数?

java - 错误 StatusLogger 捕获 java.lang.AbstractMethodError

php - MySQL - 比较来自 Ajax 数组的值,如果不在表中则删除它们

PHP 用双引号包裹一个字符串

python - 使用 Python 删除子字符串

C++:使用 std::string 在内存中移动 jpeg 是否安全?