gpt4 book ai didi

Java 在初始化时将 var 添加到对象

转载 作者:太空宇宙 更新时间:2023-11-04 09:54:47 26 4
gpt4 key购买 nike

我想在初始化时向我的对象“yamlFile”添加一个新对象。

public yamlFile config = new yamlFile() {

protected ServerGroup GROUP_BELOW_16, GROUP_16, GROUP_17, GROUP_18, GROUP_ABOVE_18;

@Override
public void load() {
super.load();
try {
GROUP_BELOW_16 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.-16"));
GROUP_16 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.16"));
GROUP_17 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.17"));
GROUP_18 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.18"));
GROUP_ABOVE_18 = Bot.getServergroupById(Bot.getFileManager().getConfigConfiguration().getInt("age.+18"));
} catch (final NullPointerException e) {
Bot.getLogger().warning("Path existiert nicht");
e.printStackTrace();
} catch (final Exception e) {
e.printStackTrace();
}

}

}.setFolder("data").setFilename("config.yml").build();

因此,在对象 yamlFile 中没有“ServerGroup”对象。所以我想补充一些。所以我在初始化时添加了它们,但无法从其他任何地方访问它们。所以我不能在其他方法中使用它们,例如:

public void method(){
config.GROUP_16 = null;
}

我的问题:我必须做什么才能使 ServerGroup 对象可访问,而不在“原始”yamlFile 类中实现它们?

原始yamlFile类:

public class yamlFile {

/**
*
* var methods
*
**/
private String folder;
private String filename;

public String getFolder() {
return folder;
}

public yamlFile setFolder(String folder) {
this.folder = folder;
return this;
}

public String getFilename() {
return filename;
}

public yamlFile setFilename(String filename) {
this.filename = filename;
return this;
}

/**
*
* file methods
*
**/
private File file;

public yamlFile set() {
file = new File(getFolder(), getFilename());
return this;
}

final private YamlConfiguration yamlConfiguration = new YamlConfiguration();

/**
* Checks for the specific file if it exists.
*/
public void fileExists() throws FileNotFoundException {
try {

if (!file.exists()) {
yamlConfiguration.options().copyDefaults(true);
file = new File(getFolder(), getFilename());
yamlConfiguration.save(file);
}
} catch (final NullPointerException e) {
e.printStackTrace();
} catch (final IllegalArgumentException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
}
}

public void load() {
try {
fileExists();
} catch (final FileNotFoundException e) {
Bot.getLogger().info("Creating file: " + getFolder() + "/" + getFilename());
}
try {

yamlConfiguration.load(file);

} catch (final FileNotFoundException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
} catch (final InvalidConfigurationException e) {
e.printStackTrace();
}
}

public void save() {
try {
yamlConfiguration.save(file);
} catch (final IOException e) {
e.printStackTrace();
}
}

public YamlConfiguration getFile() {
return yamlConfiguration;
}

}

最佳答案

我将创建一个扩展 yamlFile 的接口(interface)(例如 ServerGroupYamlFile)。该接口(interface)将包含您的方法。

public ServerGroup getGroup16(){
return config.GROUP_16;
}

然后,您实现您的界面:

public yamlFile config = new ServerGroupYamlFile() { ....

然后您可以灵活地向 yaml 类添加任何方法。

如果组列表是动态的,则只需在接口(interface)上提供一个返回组列表的方法即可。

public List<ServerGroup> getGroups(){
return yourGroups;
}

关于Java 在初始化时将 var 添加到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54296351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com