繁体   English   中英

存储应用程序初始化数据的方式和位置?

[英]How and where to store application initialization data?

我正在尝试使用Spring MVC开发游戏。

例如,我有一个Monster类。

在春天初始化和存储怪物数据的最佳方法是什么?

一种选择是在一些Init java文件中创建所有Monster实例并创建静态数组,以便每个类都可以访问它们,但这似乎是一种错误的方法。

Monster的示例: String name, int hp, int defence, int attack

另一方面,我应该使用XML或属性文件来保存我的所有MonsterItem信息吗? 将来轻松添加新ItemsMonsters会很不错。

这种春季工作的常用技术是什么?

我试着尽可能清楚地解释,问一下有什么不清楚的地方。

JAXB怎么样? 看看如何轻松编写配置并将其读回:

import java.io.*;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;

class Config {
    @XmlElementWrapper(name="monsters")
    @XmlElement(name="monster")
    public List<Monster> monsters = new ArrayList<Monster>();
}

class Monster {
    public String name = "Test";
}

public class Test1 {

    public static void main(String[] args) throws Exception {
        Config cfg = new Config();
        cfg.monsters.add(new Monster());
        //save 
        OutputStream os = new FileOutputStream("conf.xml");
        JAXB.marshal(cfg, os);
        // read 
        cfg = JAXB.unmarshal(new FileInputStream("conf.xml"), Config.class);
    }
}

conf.xml中

<config>
    <monsters>
        <monster>
            <name>Test</name>
        </monster>
    </monsters>
</config>

暂无
暂无

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

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