解析后完成对象

时间:2012-09-25 16:19:36

标签: java parsing

假设我有一个通过解析文档生成的对象数组。这些对象如下所示:

Object{
  id
  text
  anotherProperties
}

前两个属性(id和text)是在解析过程中设置的,但现在我想添加另一个属性(附加属性),这些属性在解析过程中无法设置,因为它太复杂而无法确定它们,但取决于在文本上。 我怎样才能以优雅的方式实现这一目标? 在Java? 感谢您的回复

1 个答案:

答案 0 :(得分:1)

也许使用带有Integer键(您的id)的HashMap和一个由text和anotherProperites组成的DocProperites值。

然后,当您准备好设置anotherProperties时,您可以从HashMap中检索该对象,然后进行设置。

例如

Map<Integer, DocProperties> map = new HashMap();

和DocProperties是

public class DocProperties {
    private String text;
    private String anotherProperties;
    //plus the usual setters, getters and ctor
}

然后当你想设置anotherProperties时,你可以调用

map.get(key).setAnotherProperties(....);

如果你想要更动态的东西,那么你可以使用另一个HashMap而不是DocProperties。然后,HashMap可以在解析时添加和删除键。我不建议这样做,因为代码可能变得非常混乱和错误。

相关问题