Java实例化列表

编程入门 行业动态 更新时间:2024-10-25 00:32:22
本文介绍了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.

推荐答案

您不能存储类型,但是可以存储 Class 对象.当JVM在类路径上加载并初始化一个类时,它将为该类创建一个 Class 对象.

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();

如果没有无参数构造函数,则需要获取 Class 对象中的构造函数 引用.

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

如果您需要调用方法(是否为 static ),则需要获取 Method 参考.通读 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.

  • 什么是反射,为什么反射有用?
  • Java反射性能
  • 为什么要使用反射?
  • Java反射:为什么这么慢?
  • 为什么是Java反射包被认为是力量的阴暗面?

在这种情况下,可以很好地使用 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实例化列表

本文发布于:2023-08-04 17:39:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1298299.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   列表   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!