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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12063292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:27:43  来源:igfitidea点击:

How to pass a static class as an argument in Java

javadesign-patterns

提问by user491880

I have a base class called "Entity" which has a static method called "construct" that returns an Entity instance. I have several different subclasses of this class (for demonstration assume we have "Fruit" and "Vegetable" as subclasses). I would like to be able to do something along the following lines:

我有一个名为“Entity”的基类,它有一个名为“construct”的静态方法,它返回一个实体实例。我有这个类的几个不同的子类(为了演示,假设我们有“水果”和“蔬菜”作为子类)。我希望能够按照以下方式做一些事情:

Entity a = someFunction(Fruit, textfile)

someFunction would then pass textfile to Fruit.construct and return the Entity generated. Is there a simple way to do this?

someFunction 然后将文本文件传递给 Fruit.construct 并返回生成的实体。有没有一种简单的方法可以做到这一点?

采纳答案by Cratylus

Use a factory patterninstead.
Pass the text file to the factory method that will use it to return the proper concrete instance of Entity

改用工厂模式
将文本文件传递给工厂方法,该方法将使用它返回正确的具体实例Entity

回答by Shivan Dragon

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;
}

回答by Marko Topolnik

Pass Fruit.classto the function and then use reflection on that class object to invoke the proper constructor. Note that this will couple your superclass quite tightly to its subclasses by demanding that constructor to exist.

传递Fruit.class给函数,然后在该类对象上使用反射来调用正确的构造函数。请注意,通过要求构造函数存在,这会将您的超类与其子类非常紧密地耦合在一起。

回答by Edwin Buck

Fruitin your example is a type, and while Fruit.eat()might refer to a static method, Fruitis not a "static class".

Fruit在您的示例中是一种类型,虽然Fruit.eat()可能指的是静态方法,但Fruit不是“静态类”。

There is a "class Object" which is actually an Objectthat representsthe class. Pass it instead. To get to it, they syntax Fruit.classis used.

有一个“类对象”,这实际上是一个Object代表的类。而是通过它。为了达到它,使用了它们的语法Fruit.class

回答by parsifal

You are trying to implement an object-oriented design pattern, Strategy, using procedural code. Don't do it.

您正在尝试使用过程代码实现面向对象的设计模式Strategy。不要这样做。

Instead, create an interface called EntityConstructor, which defines the method construct(). Make Fruitand Vegetableimplement that interface. Then change someFunction()to take an instance of that interface.

相反,创建一个名为 的接口EntityConstructor,它定义了方法construct()。制作FruitVegetable实现该接口。然后更改someFunction()为采用该接口的实例。

回答by joel1di1

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

你应该看看

回答by Steven Mastandrea

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() 方法的任何类)上的构造方法。