简体   繁体   中英

JAVA Using a variable value as an Instance name

So basically Im reading a file , each line of this file contains description for from 3 to 5 instances of two types of objects with 2 to 4 being first type of object and last one being another type . The file can contain up to 500 lines . I am going to reuse each object from 1 to unknown but on scale of hundrets of times and I need to track their status .

While I can all the data into an array and use only 5 objects changing their values constantly that will make it rather hard to track status of each combination of their parameters .

What I want to is to create my instances with names such as FromLine1Obj1 , FromLine10Obj3 .

I failed to mention the fact that each set of objects from a single line should also create a new thread and this set is proccesed in that thread .

You need to use a Map.

Map<String, MyObject> map = new ....

map.put("FromLine1Obj1", new MyObject());

MyObject mo = map.get("FromLine1Obj1");

如果您一次读取该文件,但它毕竟不会改变-您可能需要进行一些代码生成,例如http://cglib.sourceforge.net/

您不能使用以名称为关键字的地图吗?

您可以将解析结果存储在映射中,使用所需的名称作为关键字,并使用创建的实例的值作为值,这是Java中名称间接处理的一种简便方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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