简体   繁体   English

将 class 作为参数传递给方法,然后调用 static 方法

[英]Passing a class as argument to a method, then calling static methods

I have a use case with a class existing in 2 versions of a package.我有一个 class 的用例存在于 package 的 2 个版本中。

package packageV1;

public class MyClass extends BaseClass{

 public static String example(){
      return "Version1";
    }
}
package packageV2;

public class MyClass extends BaseClass{

 public static String example(){
     return "Version2";
      }
}

So far so good (I believe).到目前为止一切顺利(我相信)。

Then I have an application using that class, and to avoid rewriting the application for the different package version, I want to pass the class that should be use (ie for the package of interest) as argument to the application. Then I have an application using that class, and to avoid rewriting the application for the different package version, I want to pass the class that should be use (ie for the package of interest) as argument to the application. So something like所以像

public class Application{
      
      private Class<BaseClass> selectedClass;

      public void Application(Class<BaseClass> selectedClass){
               this.selectedClass = selectedClass;
               this.selectedClass.example();  // not possible
               }

}

I believe I could call this.selectedClass.example();我相信我可以调用this.selectedClass.example(); if I were passing an instance of MyClass in the constructor, but then I would call static methods through a instance object, not nice right?如果我在构造函数中传递了MyClass的实例,但是我会通过实例 object 调用 static 方法,不是很好吗?

On the other hand, in the example above selectedClass is a Class object, so I can't call the static method example as above.另一方面,在上面的示例中selectedClass是 Class object,所以我不能像上面那样调用 static 方法example

Does this mean I should use reflection?这是否意味着我应该使用反射? like selectedClass.getMethod(name, parameterTypes) .selectedClass.getMethod(name, parameterTypes) Looks overly complicated to me.对我来说看起来过于复杂。

Or is there a better design?还是有更好的设计?

Using a static method this way is not a good design, and not according to the Object Orinted principles.使用 static 方法这种方式不是一个好的设计,也不符合 Object Orinted 原则。

My tip is to try changing "example()" to be a regular method, and not static.我的建议是尝试将“example()”更改为常规方法,而不是 static。

@javadev is right. @javadev 是对的。 Using reflection is almost always a really bad idea.使用反射几乎总是一个非常糟糕的主意。 It is that which over complicates.这是过于复杂的事情。

There is no need for reflection here.这里不需要反思。 The ability to invoke static methods on instances was rapidly realised to be a design error.在实例上调用 static 方法的能力很快被认为是设计错误。 So much so that there is a subsequent highly unorthogonal design choice in that it does not work for static methods that are members of interfaces.如此之多以至于随后出现了高度非正交的设计选择,因为它不适用于作为接口成员的 static 方法。

The simple solution is to move the static methods to instance methods of stateless objects.简单的解决方案是将 static 方法移动到无状态对象的实例方法中。 No Class , or other reflection, necessary.不需要Class或其他反射。 This is an application of the Strategy design pattern.这是策略设计模式的一个应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM