簡體   English   中英

有沒有更好的方法來實現我在多個類中使用的 static 方法?

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

我有這個 static 方法,它通過給定的參數從 yml 文件返回一個字符串。 我在 20 多個課程中使用它,目前我只是將此代碼粘貼到每個 class 中。 有沒有更好的方法可以從每個 class 訪問該方法,而無需將其全部復制粘貼? 提前致謝。

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

您可以在接受ConfigMgrStringutil class 中創建 static 方法,因此您可以傳遞適當的ConfigMgr

public class YamlUtil {

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM