How do I invoke a Java method when given the method name as a string?

前端 未结 21 2222
耶瑟儿~
耶瑟儿~ 2020-11-21 04:50

If I have two variables:

Object obj;
String methodName = \"getName\";

Without knowing the class of obj, how can I call the met

21条回答
  •  面向向阳花
    2020-11-21 05:10

    Please refer following code may help you.

    public static Method method[];
    public static MethodClass obj;
    public static String testMethod="A";
    
    public static void main(String args[]) 
    {
        obj=new MethodClass();
        method=obj.getClass().getMethods();
        try
        {
            for(int i=0;i

    Thanks....

提交回复
热议问题