简体   繁体   English

根据条件调用某些方法

[英]Call certain method based of condition

I recently wrote this piece of code: 我最近写了这段代码:

  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);
    }
  }

As you can see, it is super repetitive and very ugly. 如您所见,它是超级重复的并且非常难看。

Is there a better way it can be written? 有没有更好的书写方式?


plugin.getConfig() returns one of these . plugin.getConfig()返回其中之一 I want to make a method that, when given a path ( key ) which points at some value in a YAML file, I can return that value no matter what its type is. 我想要一种方法,当给定一个指向YAML文件中某个值的路径( key )时,无论其类型是什么,我都可以返回该值。

Instead of all of this mumbo jumbo, why don't you simply write this: 而不是所有这些庞然大物,为什么不简单地这样写:

public Object getProperty(String key) {
    return this.plugin.getConfig().get(key);
}

This is especially because you are in the end returning just an Object . 尤其是因为最终您只返回一个Object So better return what you get from the YAML. 因此,最好返回您从YAML获得的收益。

If you do want to make your client's life easy, try exposing the methods of ConfigurationSection individually rather than combine them. 如果您确实想让客户的生活变得轻松,请尝试单独公开ConfigurationSection的方法,而不要结合使用。

The standard practice here, is dependent on the type of client for this code. 此处的标准做法取决于此代码的客户端类型。 If the client will directly use the various types of properties by casting them to the actual classes, then you expose the different methods from ConfigurationSection . 如果客户端通过将各种类型的属性强制转换为实际的类来直接使用它们,则可以从ConfigurationSection公开不同的方法。 however, if the immediate client is simply going to pass the properties on to other classes, then its best to expose just the single method 但是,如果直接客户只是将属性传递给其他类,则最好只公开单个方法

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

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