繁体   English   中英

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

[英]How to invoke a Java method that is in a Array of Strings? (java.lang.NoSuchMethodException)

我有一个带有Method namesStrings Array ,我试图在循环中调用它。 我已经检索了类中所有变量的名称并添加了所需的文本以填充整个函数并将整个字符串添加到列表中:

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)

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

为什么它不接受 String 作为变量?

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

1 - 您的 Telegramm 课程:

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();
        }

暂无
暂无

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

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