【问题标题】:How to give an abstract parameter object to a method of an abstract class?如何将抽象参数对象赋予抽象类的方法?
【发布时间】:2022-12-17 07:22:29
【问题描述】:

如何使用 Javas Reflection API 为方法提供参数?

这个想法是让许多参数类(POJO),每个类都有自己的服务班级。这服务类有一个方法“保存”来保存参数目的。

班级文件导入器应该只按名称使用参数和服务。

这是经过大量简化的代码 sn-p。

class Param extends AbstractParam {
... class body ...
}

class Service extends BaseService {
    ConcreteParam save(ConcreteParam param) {
        ... method body. EntityManager omitted ... }
}

class FileImporter {
    void importJson(String serviceName, String paramName, Map<String,Object> jsonMap ) {
            //--- Build service object
            Class<AbstractService> serviceClass =
                    (Class<AbstractService>) Class.forName( serviceName );
            Constructor<?> ctor =
                    serviceClass.getConstructor( (Class<?>[]) null );
            AbstractService service = (AbstractService) ctor.newInstance();

            //--- Build parameter constructor for reuse
            Class<AbstractParameter> paramClass =
                (Class<AbstractParameter>) Class.forName( paramName );
            paramConstructor = paramClass .getConstructor( (Class<?>[]) null );
            
            //--- Build parameter and fill from map
            AbstractParameter param = paramConstructor.newInstance();
            BeanHelper.populate( param, entityMap ); // populates param recursively
            param = service.save( param );
}

这种方法以编译器错误“方法‘保存’不适用于参数”结束。

【问题讨论】:

    标签: java reflection abstract-class


    【解决方案1】:

    要使用 Java Reflection API 调用方法并向其传递参数,您可以使用 java.lang.reflect.Method.invoke() 方法。该方法接受一个对象实例(非静态方法的 this 对象),一个表示方法参数的对象数组,并返回一个表示方法返回值的对象。

    下面是一个示例,说明如何使用反射 API 调用具有单个参数的方法:

    import java.lang.reflect.Method;
    
    public class ReflectionExample {
    
        public static void main(String[] args) throws Exception {
            // Get the class object for the Example class
            Class<?> exampleClass = Example.class;
    
            // Get the Method object for the sayHello method
            Method sayHelloMethod = exampleClass.getMethod("sayHello", String.class);
    
            // Create an instance of the Example class
            Example example = exampleClass.newInstance();
    
            // Invoke the sayHello method and pass it the "World" parameter
            String result = (String) sayHelloMethod.invoke(example, "World");
    
            // Print the result
            System.out.println(result);
        }
    }
    
    class Example {
        public String sayHello(String name) {
            return "Hello, " + name + "!";
        }
    }
    

    这个例子将打印“Hello, World!”到控制台。

    如果要将多个参数传递给一个方法,只需将它们添加到作为第二个参数传递给 invoke() 的对象数组中即可。例如:

    import java.lang.reflect.Method;
    
    public class ReflectionExample {
    
        public static void main(String[] args) throws Exception {
            // Get the class object for the Example class
            Class<?> exampleClass = Example.class;
    
            // Get the Method object for the sayHello method
            Method sayHelloMethod = exampleClass.getMethod("sayHello", String.class, int.class);
    
            // Create an instance of the Example class
            Example example = exampleClass.newInstance();
    
            // Invoke the sayHello method and pass it the "World" and 42 parameters
            String result = (String) sayHelloMethod.invoke(example, "World", 42);
    
            // Print the result
            System.out.println(result);
        }
    }
    
    class Example {
        public String sayHello(String name, int num) {
            return "Hello, " + name + "! Num: " + num;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-21
      • 1970-01-01
      • 2012-09-16
      相关资源
      最近更新 更多