简体   繁体   English

如何在Java中将静态类作为参数传递

[英]How to pass a static class as an argument in Java

I have a base class called "Entity" which has a static method called "construct" that returns an Entity instance. 我有一个名为“Entity”的基类,它有一个名为“construct”的静态方法,它返回一个Entity实例。 I have several different subclasses of this class (for demonstration assume we have "Fruit" and "Vegetable" as subclasses). 我有这个类的几个不同的子类(为了演示假设我们有“Fruit”和“Vegetable”作为子类)。 I would like to be able to do something along the following lines: 我希望能够做到以下几点:

Entity a = someFunction(Fruit, textfile)

someFunction would then pass textfile to Fruit.construct and return the Entity generated. someFunction然后将文本文件传递给Fruit.construct并返回生成的实体。 Is there a simple way to do this? 有一个简单的方法吗?

Use a factory pattern instead. 请改用工厂模式
Pass the text file to the factory method that will use it to return the proper concrete instance of Entity 将文本文件传递给工厂方法,该方法将使用它返回Entity的正确具体实例

You mean something like this: 你的意思是这样的:

public <T> T someFunction(Class<T> clazz, String textFile) throws Throwable {
 return clazz.newInstance();
}

The above code will use the no-arguments Constructor of the class (assuming there's one). 上面的代码将使用类的无参数构造函数(假设有一个)。

If your class needs to be instantiated with a specific constructor, you can do follow this example: 如果您的类需要使用特定构造函数进行实例化,则可以执行以下示例:

public <T> T someFunction(Class<T> clazz, String textFile) throws Throwable {
  // Here I am assuming the the clazz Class has a constructor that takes a String as argument.
  Constructor<T> constructor = clazz.getConstructor(new Class[]{String.class});
  T obj = constructor.newInstance(textFile);
  return obj;
}

Pass Fruit.class to the function and then use reflection on that class object to invoke the proper constructor. Fruit.class传递给函数,然后对该类对象使用反射来调用正确的构造函数。 Note that this will couple your superclass quite tightly to its subclasses by demanding that constructor to exist. 请注意,这会通过要求构造函数存在来将您的超类与其子类紧密地耦合在一起。

Fruit in your example is a type, and while Fruit.eat() might refer to a static method, Fruit is not a "static class". 您的示例中的Fruit是一个类型,而Fruit.eat()可能引用静态方法,而Fruit不是“静态类”。

There is a "class Object" which is actually an Object that represents the class. 有一个“类对象”,它实际上是一个表示类的Object Pass it instead. 通过它而不是。 To get to it, they syntax Fruit.class is used. 为了实现它,使用了它们的语法Fruit.class

You are trying to implement an object-oriented design pattern, Strategy , using procedural code. 您正在尝试使用过程代码实现面向对象的设计模式Strategy Don't do it. 不要这样做。

Instead, create an interface called EntityConstructor , which defines the method construct() . 相反,创建一个名为EntityConstructor的接口,该接口定义方法construct() Make Fruit and Vegetable implement that interface. 使FruitVegetable实现该界面。 Then change someFunction() to take an instance of that interface. 然后更改someFunction()以获取该接口的实例。

Here is a implementation : 这是一个实现:

public <T extends Entity> T someMethod(Class<T> entityClass, File file) throws InstantiationException, IllegalAccessException {
        T newEntity = entityClass.newInstance();
        // do something with file
        // ...
        return newEntity;
    }

You should look to 你应该看看

Static methods are not inherited, per se, in that if your code has Entity.construct(...) it will not dynamically link that to the sub class. 静态方法本身不是继承的,因为如果您的代码具有Entity.construct(...),它将不会动态地将其链接到子类。

The best way to accomplish what you are asking for is to use reflection to invoke the construct method on the Fruit class (or whatever class was passed into the someFunction() method. 完成所要求的最好方法是使用反射来调用Fruit类上的构造方法(或者将任何类传递给someFunction()方法)。

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

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