Run a java method by passing class name and method name as parameter

前端 未结 3 663
情深已故
情深已故 2021-01-14 09:04

I am trying to make a program that executes a particular method when the class name and method name are passed as String parameter to the caller. Consider the code below. I

3条回答
  •  醉梦人生
    2021-01-14 09:27

    What to do now???

    You can now call the method on your carObj like this:

    Method method = carObj.getClass().getMethod(methodName);
    method.invoke(carObj);
    

    See also:

    Method.invoke

提交回复
热议问题