我的 bukkit 插件有很大的配置文件,但我不知道如何为它创建一个配置类。如果我用这么多的变量创建一个类,那么很难使用和阅读/编辑。
所以我有使用内在祝福的新想法,但性能等呢?
该示例配置文件:(在 YAML 中,实际配置文件要大得多)http://pastebin.com/09WMq5qG
以及我对该示例的代码:
public class Cfg {
protected General general;
protected General2 general2;
public Cfg(ConfigurationSection cfg) {
general = new General(cfg.getConfigurationSection("General"));
general2 = new General2(cfg.getConfigurationSection("General2"));
}
public class General {
protected Gen2 gen2;
protected Gen5 gen5;
protected int op1;
protected String op2;
public General(ConfigurationSection general) {
this.op1 = general.getInt("op1");
this.op2 = general.getString("op2");
gen2 = new Gen2(general.getConfigurationSection("Gen2"));
gen5 = new Gen5(general.getConfigurationSection("Gen5"));
}
public class Gen2 {
protected Gen3 gen3;
protected Gen4 gen4;
protected int lol;
protected String edrd;
public Gen2(ConfigurationSection gen2) {
this.lol = gen2.getInt("lol");
this.edrd = gen2.getString("edrd");
gen3 = new Gen3(gen2.getConfigurationSection("Gen3"));
gen4 = new Gen4(gen2.getConfigurationSection("Gen4"));
}
public class Gen3 {
protected int dsdgdf;
protected String djkw4g;
public Gen3(ConfigurationSection gen3) {
this.dsdgdf = gen3.getInt("dsdgdf");
this.djkw4g = gen3.getString("djkw4g");
}
}
public class Gen4 {
protected int dsdgdf;
protected String djkw4g;
public Gen4(ConfigurationSection gen4) {
this.dsdgdf = gen4.getInt("dsdgdf");
this.djkw4g = gen4.getString("djkw4g");
}
}
}
public class Gen5 {
protected Gen6 gen6;
protected Gen7 gen7;
protected int lol;
protected String edrd;
public Gen5(ConfigurationSection gen5) {
this.lol = gen5.getInt("lol");
this.edrd = gen5.getString("edrd");
gen6 = new Gen6(gen5.getConfigurationSection("Gen6"));
gen7 = new Gen7(gen5.getConfigurationSection("Gen7"));
}
public class Gen6 {
protected int dsdgdf;
protected String djkw4g;
public Gen6(ConfigurationSection gen6) {
this.dsdgdf = gen6.getInt("dsdgdf");
this.djkw4g = gen6.getString("djkw4g");
}
}
public class Gen7 {
protected int dsdgdf;
protected String djkw4g;
public Gen7(ConfigurationSection gen7) {
this.dsdgdf = gen7.getInt("dsdgdf");
this.djkw4g = gen7.getString("djkw4g");
}
}
}
}
public class General2 {
protected Gen2 gen2;
protected Gen5 gen5;
protected int op1;
protected String op2;
public General2(ConfigurationSection general2) {
this.op1 = general2.getInt("op1");
this.op2 = general2.getString("op2");
gen2 = new Gen2(general2.getConfigurationSection("Gen2"));
gen5 = new Gen5(general2.getConfigurationSection("Gen5"));
}
public class Gen2 {
protected Gen3 gen3;
protected Gen4 gen4;
protected int lol;
protected String edrd;
public Gen2(ConfigurationSection gen2) {
this.lol = gen2.getInt("lol");
this.edrd = gen2.getString("edrd");
gen3 = new Gen3(gen2.getConfigurationSection("Gen3"));
gen4 = new Gen4(gen2.getConfigurationSection("Gen4"));
}
public class Gen3 {
protected int dsdgdf;
protected String djkw4g;
public Gen3(ConfigurationSection gen3) {
this.dsdgdf = gen3.getInt("dsdgdf");
this.djkw4g = gen3.getString("djkw4g");
}
}
public class Gen4 {
protected int dsdgdf;
protected String djkw4g;
public Gen4(ConfigurationSection gen4) {
this.dsdgdf = gen4.getInt("dsdgdf");
this.djkw4g = gen4.getString("djkw4g");
}
}
}
public class Gen5 {
protected Gen6 gen6;
protected Gen7 gen7;
protected int lol;
protected String edrd;
public Gen5(ConfigurationSection gen5) {
this.lol = gen5.getInt("lol");
this.edrd = gen5.getString("edrd");
gen6 = new Gen6(gen5.getConfigurationSection("Gen6"));
gen7 = new Gen7(gen5.getConfigurationSection("Gen7"));
}
public class Gen6 {
protected int dsdgdf;
protected String djkw4g;
public Gen6(ConfigurationSection gen6) {
this.dsdgdf = gen6.getInt("dsdgdf");
this.djkw4g = gen6.getString("djkw4g");
}
}
public class Gen7 {
protected int dsdgdf;
protected String djkw4g;
public Gen7(ConfigurationSection gen7) {
this.dsdgdf = gen7.getInt("dsdgdf");
this.djkw4g = gen7.getString("djkw4g");
}
}
}
}
}
对我来说,使用它比将所有变量存储在一个类中更容易,但是性能如何?这将是好方法还是只是减慢代码速度?
对不起我的英语。
做什么:加载配置文件并将整个内容存储在一个变量中(array,ArrayList,List,Hashmap,任何适合你特定需求的东西)。在此之后,您的所有班级都可以阅读它。您可以在配置文件内容中强制实施明确的顺序,以便程序的各个部分确切地知道,例如,它们必须在哪个行/数组索引范围内搜索所需的信息。您还可以定义一个将所有配置详细信息分发到程序先前初始化部分的方法。这一切都取决于你。因为你没有指出你打算做什么,我也不能在这里做更详细的事情。
但是,我建议您不要在类内的类中创建类等。Java 为您提供了出色的面向对象工具,可帮助您以小的可读部分组织代码。