简体   繁体   中英

Is there a better way to implement static method that I use in multiple classes?

I have this static method which returns a string from a yml file by given parameters. I use this in over 20 classes and currently I just paste this code into each class. Is there better way to access that method from each class without needing to copy paste it all over? Thanks in advance.

private static String msgYml(String message) {
        return ConfigMgr.getMessages().getString(message);
    }

You can create a static method in util class that accepts ConfigMgr and String , so you can pass the appropriate ConfigMgr

public class YamlUtil {

   public static String getYmlProps(ConfigMgr configMgr, String prop) {
       return configMgr.getMessages().getString(prop);
      }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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