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

前端 未结 21 2159
耶瑟儿~
耶瑟儿~ 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:22

    For those who want a straight-forward code example in Java 7:

    Dog class:

    package com.mypackage.bean;
    
    public class Dog {
        private String name;
        private int age;
    
        public Dog() {
            // empty constructor
        }
    
        public Dog(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public void printDog(String name, int age) {
            System.out.println(name + " is " + age + " year(s) old.");
        }
    }
    

    ReflectionDemo class:

    package com.mypackage.demo;
    
    import java.lang.reflect.*;
    
    public class ReflectionDemo {
    
        public static void main(String[] args) throws Exception {
            String dogClassName = "com.mypackage.bean.Dog";
            Class dogClass = Class.forName(dogClassName); // convert string classname to class
            Object dog = dogClass.newInstance(); // invoke empty constructor
    
            String methodName = "";
    
            // with single parameter, return void
            methodName = "setName";
            Method setNameMethod = dog.getClass().getMethod(methodName, String.class);
            setNameMethod.invoke(dog, "Mishka"); // pass arg
    
            // without parameters, return string
            methodName = "getName";
            Method getNameMethod = dog.getClass().getMethod(methodName);
            String name = (String) getNameMethod.invoke(dog); // explicit cast
    
            // with multiple parameters
            methodName = "printDog";
            Class[] paramTypes = {String.class, int.class};
            Method printDogMethod = dog.getClass().getMethod(methodName, paramTypes);
            printDogMethod.invoke(dog, name, 3); // pass args
        }
    }
    

    Output: Mishka is 3 year(s) old.


    You can invoke the constructor with parameters this way:

    Constructor dogConstructor = dogClass.getConstructor(String.class, int.class);
    Object dog = dogConstructor.newInstance("Hachiko", 10);
    

    Alternatively, you can remove

    String dogClassName = "com.mypackage.bean.Dog";
    Class dogClass = Class.forName(dogClassName);
    Object dog = dogClass.newInstance();
    

    and do

    Dog dog = new Dog();
    
    Method method = Dog.class.getMethod(methodName, ...);
    method.invoke(dog, ...);
    

    Suggested reading: Creating New Class Instances

提交回复
热议问题