Java实例化列表 [英] Java list of uninstantiated classes

查看:34
本文介绍了Java实例化列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以构造一个 List< Object> ,其中的元素是未实例化的类,目的是获取列表元素之一并从该类中运行静态方法或实例化新实例那个班的?还有,这怎么可能?它是多么不明智(为什么)?

Is it possible to construct a List<Object> wherein the elements are uninstantiated classes, with the intention of getting one of the list elements and running a static method from that class or instantiating a new instance of that class? Also, how might one do this and how inadvisable is it (and why)?

添加一些上下文:我想创建一个生成随机城市的应用程序,将建筑物放置在城市中,其中每个建筑物都是许多建筑物类之一的实例,每个类都继承自抽象基类.为了选择适合某块土地的建筑物,程序可以遍历所有可能建筑物类别的列表,检查该建筑物是否满足要求的参数(最小/最大高度,占地面积,形状等)或这些类可以以其他方式存储,可能使用映射或其他结构.最重要的是,我需要存储(引用?)到未实例化的类.

To add some context: I want to make an application which generates a randomized city, placing buildings in the city, where each building is an instance of one of the many building classes, each inheriting from an abstract base class. To choose a building appropriate for a certain piece of land, the program may iterate over a list of all possible building classes, checking whether the required parameters can be met for that building (minimum / maximum height, footprint, shape, etc.) or these classes may be stored in some other manner, possibly using maps or some other structures. The bottom line is that I need to store (refferences?) to uninstantiated classes.

推荐答案

您不能存储类型,但是可以存储

You cannot store types, but you can store the Class object for each class loaded by the JVM. When the JVM loads and initializes a class on the classpath, it creates a single Class object for that class.

Class 类使您可以访问许多实例方法来执行

The Class class gives you access to a number of instance methods for performing reflection on your program.

还有,这怎么可能?它是多么不可取(为什么)?

Also, how might one do this and how inadvisable is it (and why)?

例如

List<Class<?>> classes = new ArrayList<>();
classes.add(Example.class);

现在,您可以根据需要执行的操作,遍历元素并调用不同的方法.如果您要创建一个新实例(并且您的类具有无参数构造函数),则可以

Now you can go through the elements and call different methods depending on what you want to do. If you want to create a new instance (and your class has a no-arg constructor), you can do

classes.get(0).newInstance();

如果没有无参数构造函数,则需要获取

If you don't have a no-argument constructor, you will need to get Constructor references from the Class object.

如果您需要调用方法(是否为 static ),则需要获取 javadoc .

If you need to invoke methods (whether static or not), you need to get a Method reference. Read through the javadoc.

这是多么不明智(为什么)?

how inadvisable is it (and why)?

有些事情你不能没有反思,但通常不鼓励这样做.

There are some things you cannot do without reflection, but it is generally discouraged.

在这种情况下,可以很好地使用 Factory 模式来实现目标时,应尽量减少使用Reflection.

You should try to minimize using Reflection when you can very well use the Factory pattern to achieve your goals in this situation.

用户user2864740建议

我将有一个知道如何创建工厂的工厂"列表.适当的对象"(如果可行的话)-实际取决于数据来自哪里以及它起什么作用.

I would have a list of "factories that know how to create the appropriate object", if practical - really depends upon where this data comes from and what role it has.

这篇关于Java实例化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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