在运行时使用反射来实例化未知类的对象的正确方法是什么? [英] What's the proper way to use reflection to instantiate objects of unknown classes at runtime?

查看:88
本文介绍了在运行时使用反射来实例化未知类的对象的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Configuration Loader类,这样我就可以通过外部文本文件(config.txt)更改程序的参数,而不必在每次更改时重新编译我的代码。

I am working on a Configuration Loader class so that I can change the parameters of my program via an external text file (config.txt) rather than having to recompile my code with every change I make.

有人建议我使用Java的Reflection来做这件事,但我对如何实际实现这一点感到困惑。

It has been suggested that I use Java's Reflection to do this, but I'm a little confused as to how I might actually implement this.

我已经能够从我的文本文件中成功提取其构造函数的类名和参数,但是如何从这个实例化对象?

I have been able to successfully extract the class name and the arguments for its constructor from my text file, but how do I go from this to an instantiated object?

这是我到目前为止的方法:

here's what I have of my method so far:

public void loadObject(String classString, HashMap hm)
  {
  String className = props.getProperty(classString);
  Class c = Class.forName(className);
  }

classString是一个包含类名的字符串,hm是一个hashmap其中类的构造函数参数映射到其预期值。

classString is a string containing the name of the class, and hm is a hashmap where the class' constructor parameters map to their intended values.

类Foo(int xPos,float yPos),xPos将映射到预期int的字符串,yPos映射到预期float的字符串。我希望能够返回,新的Foo(hm.getxPos.toInt,hm.getyPost.toFloat),但我不确定如何动态使用这样的构造函数(问题是,有多个可能的类 - 也许它是 bar 而不是 foo ,例如)。

I.e., for class Foo (int xPos, float yPos), "xPos" would map to a string of the intended int, and "yPos" maps to a string of the intended float. I want to be able to return, new Foo(hm.get"xPos".toInt, hm.get"yPost".toFloat), but I'm unsure how to dynamically use a constructor like that (the issue is, there are multiple possible classes -- perhaps it's a bar instead of a foo, for instance).

我知道可以根据classString做一个if / else,只需调用正确的构造函数以这种方式识别之后,但我希望创建一个更具可扩展性的代码,每次向程序添加新类时都不必重写。

I know that its possible to do an if/else based off the classString, and simply call the proper constructor after identifying it that way, but I am looking to create a more extensible code that doesn't have to be rewritten every time I add a new class to the program.

所有可能的对象都从单个父对象继承。

All of the possible objects inherit from a single parent object.

推荐答案

您将使用 Class.getConstructor (类<?> ... parameterTypes)获取对构造函数的引用,后跟 Constructor.newInstance(Object ... initargs)

You would use Class.getConstructor(Class<?>... parameterTypes) to get a reference to the constructor followed by Constructor.newInstance(Object... initargs).

但是我建议看一下依赖项injec诸如Spring或Guice之类的框架,因为它听起来像你正在创建的是他们所做的基本版本。

However I would suggest taking a look at a dependency injection framework such as Spring or Guice as it sounds like what you are creating is a basic version of what they do.

根据请求扩展这个答案:

Class c = Class.forName(name);
Constructor ctor = c.getConstructor(Integer.class, Integer.class);
Integer param1 = hm.get("xPos") ...;
Integer param2 = hm.get("yPos") ...;
Object instanceOfTheClass = ctor.newInstance(param1, param2);

当然不是 param1 param2 你会根据输入文件中的内容创建一个参数数组(同样适用于 getConstructor()的参数)等等。

Of course instead of param1 and param2 you would create an array of arguments based upon what was in the input file (the same goes for the arguments to getConstructor()), etc.

这篇关于在运行时使用反射来实例化未知类的对象的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆