有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java属性文件作为枚举

是否可以将属性文件转换为枚举

我有一个包含很多设置的propoEntry文件。比如

equipment.height
equipment.widht
equipment.depth 
and many more like this and not all are as simple as the example

开发商必须知道钥匙才能获得房产的价值。相反,开发人员可以输入MyPropertyEnum。键列表将显示在IDE中,就像它显示在枚举中一样

MyPropertyEnum.height

共 (3) 个答案

  1. # 1 楼答案

    我可以想象,获取IDE中所有属性的一种方法是定义一个包含所有属性的枚举,如下所示:

    public enum Settings
    {
       EQUIPMENT_HEIGHT("equipment.height", "0"),
    
       EQUIPMENT_WIDTH("equipment.width", "0"),
    
       EQUIPMENT_DEPTH("equipment.depth", "0");
    
       private String property;
    
       private String value;
    
       Settings(final String aProperty, final String aValue)
       {
          property = aProperty;
          value = aValue;
       }
    
       public String getProperty()
       {
          return property;
       }
    
       public String getValue()
       {
          return value;
       }
    
       private void setValue(final String aValue)
       {
          value = aValue;
       }
    
       public static void initialize(final Properties aPropertyTable)
       {
          for(final Settings setting : values())
          {
             final String key = setting.getProperty();
             final String defaultValue = setting.getValue();
             setting.setValue(aPropertyTable.getProperty(key, defaultValue));
          }
       }
    }
    

    枚举的初始化是自我解释的(方法initialize()

    之后,你可以这样使用它:

    Settings.EQUIPMENT_HEIGHT.getValue();
    

    添加新属性只是添加新的枚举常量

  2. # 2 楼答案

    public enum ErrorCode{
    
    
        DB_ERROR( PropertiesUtil.getProperty("DB_ERRROR_CODE"), PropertiesUtil.getProperty("DB_ERROR")),
        APP_ERROR(PropertiesUtil.getProperty("APPLICATION_ERROR_CODE"), PropertiesUtil.getProperty("APPLICATION_ERROR")),
        ERROR_FOUND(PropertiesUtil.getProperty("ERROR_FOUND_CODE"), PropertiesUtil.getProperty("ERROR_FOUND"));
    
    
        private final String errorCode;
        private final String errorDesc;
    
    
    
        private ErrorCode(String errorCode, String errorDesc) {
            this.errorCode = errorCode;
            this.errorDesc = errorDesc;
        }
    
        public String getErrorDesc() {
            return errorDesc;
        }
    
        public String getErrorCode() {
            return errorCode;
        }
    
        public static String getError(String errorCode)
        { 
            System.out.println("errorCode in Enum"+errorCode);
            System.out.println(java.util.Arrays.asList(ErrorCode.values()));
            for (ErrorCode errorEnum : ErrorCode.values()) {
                System.out.println(errorEnum.errorCode);
                System.out.println(errorEnum.errorDesc);
            if ((errorEnum.errorCode).equals(errorCode)) {
                return errorEnum.getErrorDesc();
            }
            }
            return ERROR_FOUND.getErrorDesc();
    
        }
    
    
    public class PropertiesUtil {
    static  Properties prop = new Properties();
    
        static{
    
            try {
    
                  InputStream inputStream =
                          PropertiesUtil.class.getClassLoader().getResourceAsStream("db.properties");
    
                 prop.load(inputStream);
        }catch(Exception e)
            {
            e.printStackTrace();
            }
        }
    
    
    
            public static PropertiesUtil getInstance()
            {
    
                return new PropertiesUtil();
            }
    
            public Properties getProperties()
            {
                return prop;
            }
    
            public static String getProperty(String key)
            {
                return prop.getProperty(key);
            }
    
    
    }
    
  3. # 3 楼答案

    Java有静态类型。这意味着,不能动态创建类型。因此,答案是否定的。无法将属性文件转换为枚举

    您可以做的是从该属性文件生成一个enum。或者,使用字典(地图)访问您的属性,例如:

    equipment.get("height");