根据条件调用特定方法
Call certain method based of condition
我最近写了这段代码:
public Object getProperty(String key) {
if (this.plugin.getConfig().isBoolean(key)) {
return this.plugin.getConfig().getBoolean(key);
} else if (this.plugin.getConfig().isColor(key)) {
return this.plugin.getConfig().getColor(key);
} else if (this.plugin.getConfig().isConfigurationSection(key)) {
return this.plugin.getConfig().getConfigurationSection(key);
} else if (this.plugin.getConfig().isDouble(key)) {
return this.plugin.getConfig().getDouble(key);
} else if (this.plugin.getConfig().isInt(key)) {
return this.plugin.getConfig().getInt(key);
} else if (this.plugin.getConfig().isItemStack(key)) {
return this.plugin.getConfig().getItemStack(key);
} else if (this.plugin.getConfig().isList(key)) {
return this.plugin.getConfig().getList(key);
} else if (this.plugin.getConfig().isLong(key)) {
return this.plugin.getConfig().getLong(key);
} else if (this.plugin.getConfig().isOfflinePlayer(key)) {
return this.plugin.getConfig().getOfflinePlayer(key);
} else if (this.plugin.getConfig().isPrimitiveWrapper(key)) {
return this.plugin.getConfig().getPrimitiveWrapper(key);
} else if (this.plugin.getConfig().isSet(key)) {
return this.plugin.getConfig().getSet(key);
} else if (this.plugin.getConfig().isString(key)) {
return this.plugin.getConfig().getString(key);
} else if (this.plugin.getConfig().isVector(key)) {
return this.plugin.getConfig().getVector(key);
}
}
如你所见,它超级重复而且非常丑陋。
有没有更好的写法?
plugin.getConfig()
returns one of these。我想创建一个方法,当给定一个指向 YAML 文件中某个值的路径 (key
) 时,无论其类型是什么,我都可以 return 该值。
你为什么不简单地写下这个:
public Object getProperty(String key) {
return this.plugin.getConfig().get(key);
}
这尤其是因为您最终 return 只是一个 Object
。所以更好 return 你从 YAML 中得到的东西。
如果您确实想让客户的生活更轻松,请尝试单独公开 ConfigurationSection
的方法,而不是将它们结合起来。
此处的标准做法取决于此代码的客户端类型。如果客户端将通过将它们转换为实际的 类 来直接使用各种类型的属性,那么您将公开来自 ConfigurationSection
的不同方法。但是,如果直接客户端只是将属性传递给其他 类,那么最好只公开单个方法
我最近写了这段代码:
public Object getProperty(String key) {
if (this.plugin.getConfig().isBoolean(key)) {
return this.plugin.getConfig().getBoolean(key);
} else if (this.plugin.getConfig().isColor(key)) {
return this.plugin.getConfig().getColor(key);
} else if (this.plugin.getConfig().isConfigurationSection(key)) {
return this.plugin.getConfig().getConfigurationSection(key);
} else if (this.plugin.getConfig().isDouble(key)) {
return this.plugin.getConfig().getDouble(key);
} else if (this.plugin.getConfig().isInt(key)) {
return this.plugin.getConfig().getInt(key);
} else if (this.plugin.getConfig().isItemStack(key)) {
return this.plugin.getConfig().getItemStack(key);
} else if (this.plugin.getConfig().isList(key)) {
return this.plugin.getConfig().getList(key);
} else if (this.plugin.getConfig().isLong(key)) {
return this.plugin.getConfig().getLong(key);
} else if (this.plugin.getConfig().isOfflinePlayer(key)) {
return this.plugin.getConfig().getOfflinePlayer(key);
} else if (this.plugin.getConfig().isPrimitiveWrapper(key)) {
return this.plugin.getConfig().getPrimitiveWrapper(key);
} else if (this.plugin.getConfig().isSet(key)) {
return this.plugin.getConfig().getSet(key);
} else if (this.plugin.getConfig().isString(key)) {
return this.plugin.getConfig().getString(key);
} else if (this.plugin.getConfig().isVector(key)) {
return this.plugin.getConfig().getVector(key);
}
}
如你所见,它超级重复而且非常丑陋。
有没有更好的写法?
plugin.getConfig()
returns one of these。我想创建一个方法,当给定一个指向 YAML 文件中某个值的路径 (key
) 时,无论其类型是什么,我都可以 return 该值。
你为什么不简单地写下这个:
public Object getProperty(String key) {
return this.plugin.getConfig().get(key);
}
这尤其是因为您最终 return 只是一个 Object
。所以更好 return 你从 YAML 中得到的东西。
如果您确实想让客户的生活更轻松,请尝试单独公开 ConfigurationSection
的方法,而不是将它们结合起来。
此处的标准做法取决于此代码的客户端类型。如果客户端将通过将它们转换为实际的 类 来直接使用各种类型的属性,那么您将公开来自 ConfigurationSection
的不同方法。但是,如果直接客户端只是将属性传递给其他 类,那么最好只公开单个方法