简体   繁体   English

创建没有现有类的对象

[英]Creating an object with no existing class

I would like to know if it is possible to create a "temporary" object (at runtime) of a class that does not exist, and create fields for that object. 我想知道是否可以创建一个不存在的类的“临时”对象(在运行时),并为该对象创建字段。

I'm parsing a text file for which I would need to create manually hundreds, if not more, of classes to store (and later write) all the data represented in this file. 我正在解析一个文本文件,我需要手动创建数百个(如果不是更多)类来存储(以后再写入)此文件中表示的所有数据。

That file would look like : 该文件看起来像:

person,John,28,USA,Male
person,Rebecca,25,USA,Female

... later ... ...稍后...

address,John,...
address,Rebecca,..

I would like to make a method that will, when metting a new kind of data (here, a Person or an Address), create an object of the corresponding class (Person.class or Address.class), class that doesn't exist in my src code. 我想制作一个方法,当它吸收一种新类型的数据(这里是一个Person或一个Address)时,将创建对应类(Person.class或Address.class)的对象,而该类不存在在我的src代码中。

Then, I'll split the line around (",") and for each String, create a new Field for that new Object and put the value inside. 然后,我将分割线(“,”)并为每个String创建一个新的Field,并将值放入其中。

EDIT : 编辑:

Found dexmaker that seems to be what I need, but will I be able to use the dexmaker generated class to build an output xml using JAXB/DOM or other similar implementations ? 发现dexmaker ,这似乎是我所需要的,但我将能够使用dexmaker生成的类生成使用JAXB / DOM或其他类似的实现输出XML?

Thanks 谢谢

You can use a HashMap<String, String> as your flexible object: every key (fieldname) refers to a field value. 您可以使用HashMap<String, String>作为灵活对象:每个键(fieldname)引用一个字段值。

For every type (Address, Person) add a HashMap, and for keeping trac of the type-hashmaps, well use a HashMap<String, HashMap<String, String>> :) 对于每种类型(地址,人),添加一个HashMap,并保留类型HashMap<String, HashMap<String, String>>的跟踪,那么请使用HashMap<String, HashMap<String, String>> :)

This works a bit like javascript objects which are in effect associative arrays. 这有点像实际上是关联数组的javascript对象。

You can have any class which holds String key and values in a Properties container. 您可以在Properties容器中拥有任何包含String键和值的类。 Not sure why is usefully... 不确定为什么有用...

For SQL there are ORM frameworks, which will do your job automatically, if you have linked the library and configured properly. 对于SQL,有ORM框架,如果您已链接库并正确配置,它将自动完成您的工作。

Why you need temporary object ? 为什么需要临时对象? If you really do not want to create any class in source code then , you can take a help of Collections.Use a suitable collection for keeping the data for short period of time. 如果您确实不想在源代码中创建任何类,则可以使用Collections的帮助。使用合适的collection来短时间保存数据。

They are predefined , no need to create any class for that.You can use HashMap,HashTable,etc. 它们是预定义的,无需为此创建任何类。您可以使用HashMap,HashTable等。

For storing parsing data, you can use Collections class utilities. 为了存储解析数据,可以使用Collections类实用程序。 I am also working on parsing files, i always prefer to use Map,List. 我也在解析文件,我总是喜欢使用Map,List。 Java provides these things for fast development, no time wasting. Java为快速开发提供了这些东西,没有时间浪费。 Create an object for every new data and compare these objects for storing only different typs of objects in Map.. 为每个新数据创建一个对象,并比较这些对象,以便在Map中仅存储不同类型的对象。

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

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