java - 如何使用不同的类作为方法中的参数

标签 java jakarta-ee generics coding-style

我会尽可能清楚地解释我的问题:)

我在 jar 文件中使用网络服务。在这个 jar 文件中,我将使用不同的类。每节课一年:

  • 2012 has a class with the name Service12.
  • 2013 has a class with the name Service13.
  • 2014 has a class with the name service14.

等等。

这些类仅供使用,我无法更改此 jar 文件中的任何内容。

我将在 webService 中使用关于每年的类,但不会使用 if-else,因为它是超过 20 年的服务方法。:

public myObj methodA (int year){
    ServiceXX serviceXX = myWebService.useThisService(ServiceXX.class);
}

myWebService 有一个像这样的通用方法:

public <T> T useThisService(Class<T> serviceClass) {        
    return (T) this.getService(serviceClass);
}

我如何在没有任何 if-else 的情况下对 methodA 进行编码,我可以让用户更正每个 yearServiceXX

希望它足够清楚以获得帮助。

最佳答案

你可以使用 myWebService.useThisService(Class.forName("Service"+ year)); 吗?

关于java - 如何使用不同的类作为方法中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21135384/

相关文章:

java - 是什么导致 "NoSuchMethodError: org.springframework.beans.BeanWrapper.setAutoGrowCollectionLimit(I)V?"

java - JAX-RS 2 打印 JSON 请求

java - 为什么这个函数允许我使用 lambda 表达式

java - 如果不在事务中,Hibernate session.update 不工作

java - 确保 Java 中的通用通配符匹配

java - Java中的Kotlin泛型错误

java - 使用 Talend Routine 处理 DB null

java - @PreAuthorize 安全角色注解

java - JPanel 去除颜色渐变

java - CGLib 代理和非空构造函数