how to call a java method using a variable name?

后端 未结 4 1669
傲寒
傲寒 2020-11-28 09:52

Say I have Method1(void), Method2(void)...

Is there a way i can chose one of those with a variable?

 String MyVar=2;
 MethodMyVar();
4条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 10:31

    I'm not sure how the accepted answer works for method.invoke() without first argument of static method being null(put dummy value still works though). According to The Java™ Tutorials:

    The first argument is the object instance on which this particular method is to be invoked. (If the method is static, the first argument should be null.)

    The following shows a complete examples (Main.java), for both static(by class) VS non-static(by instance), plus additional example for method with argument, import necessary class, catch exception, and also superclass method example.

    import java.lang.reflect.Method;
    import java.lang.reflect.InvocationTargetException;
    
    class Love {
       protected void Method4() {
            System.out.println("calls super protected method by instance");
        }
    
       public void Method5() {
            System.out.println("calls super public method by instance");
        }
    }
    
    class Main extends Love {
    
        static void Method2(int y) {
            System.out.println("by class: " + y);
        }
    
        void Method3(String y) {
            System.out.println(y);
        }
    
        public static void main(String[] args) {
    
            String MyVar = "2";
            String MyAnotherVar = "3";
            String MySuperVar = "4";
            String MySuperPublicMethodVar = "5";
            Main m = new Main();
    
           try {
                Method method = Main.class.getDeclaredMethod("Method" + MyVar, int.class); //by class
                Method anotherMethod = m.getClass().getDeclaredMethod("Method" + MyAnotherVar, String.class); //by instance
                Method superMethod = m.getClass().getSuperclass().getDeclaredMethod("Method" + MySuperVar); //super method by instance, can be protected
                Method superPublicMethod = m.getClass().getMethod("Method" + MySuperPublicMethodVar); //getMethod() require method defined with public, so even though sublcass calls super protected method will not works
                try {
                    method.invoke(null, 10000);//by class
                    anotherMethod.invoke(m, "by instance"); //by instance
                    superMethod.invoke(m); //super method by instance
                    superPublicMethod.invoke(m); //super's public method by instance
                } catch (InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
    
           } catch (NoSuchMethodException e) {
               throw new RuntimeException(e);
           } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
           }
        }
    }
    

    Output:

    $ javac Main.java
    $ java Main 
    by class: 10000
    by instance
    calls super protected method by instance
    calls super public method by instance
    $ 
    

提交回复
热议问题