簡體   English   中英

枚舉中的私有靜態最終變量

[英]Private static final variable inside an enum

我正在嘗試在枚舉中創建一個私有靜態最終變量,但我不斷收到編譯錯誤。 有誰知道如何解決這一問題?

此行有多個標記

  • 語法錯誤,插入“Identifier”以完成EnumConstantHeaderName
  • 語法錯誤,插入“}”以完成EnumBody
class Foo {
  ...

  public enum MyEnum {
    private static final String MY_STRING = "a string I use in a constructor";
    private static final String MY_OTHER_STRING = "a string I use in another constructor";      

    MyEnumType(1, MY_STRING),
    MyEnumType2(2, MY_STRING),
    MyEnumType3(3, MY_OTHER_STRING);

    MyEnum(int num, String str) {
      ...
    } 
  }
 ...
}

枚舉常量需要是枚舉中的第一個元素。 您編譯的代碼版本:

class Foo {

    public enum MyEnum {
        MyEnumType, MyEnumType2;

        public String bar() {
            return MY_STRING;
        }

        public String bar2() {
            return MY_STRING + "2";
        }

        private static final String MY_STRING = "a string I reuse in the enum";
    }
}

*編輯*

根據您對原始問題的編輯,我看到您要做的事情。 不可能在枚舉定義本身聲明的枚舉文字聲明中使用常量。 這是因為文字聲明必須是枚舉中的第一個元素。 這是Java語言規范的強制要求。 但有兩個快速的事情:

  1. 您正在使用private static final Strings 這對於使用字符串文字而言絕對沒有任何好處,這將解決問題。
  2. 如果你想聲明可重用的常量( public static final Strings ),那么你需要在枚舉之外這樣做。

或者,您可以將Enums聲明為類的嵌套元素,該類為您定義private static final String

一些偽代碼:

public class Foo {
    public static enum MyEnum {
        MyEnumType(0, MY_STRING), MyEnumType2(1, "Hello");

        private int ordinal;
        private String value;

        MyEnum(int ordinal, String value) {
            this.ordinal = ordinal;
            this.value = value;
        }

        public int getOrdinal() {
            return ordinal;
        }

        public String getValue() {
            return value;
        }

        public void setOrdinal(int ordinal) {
            this.ordinal = ordinal;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    private static final String MY_STRING = "a string I reuse in the enum";
}

有可能,您只需要直接引用變量。

class Foo {
  ...

  public enum MyEnum {

    MyEnumType(1, MyEnum.MY_STRING),
    MyEnumType2(2, MyEnum.MY_STRING),
    MyEnumType3(3, MyEnum.MY_OTHER_STRING);

    MyEnum(int num, String str) {
      ...
    }

     private static final String MY_STRING = "a string I use in a constructor";
     private static final String MY_OTHER_STRING = "a string I use in another constructor";  
  }
 ...
}

可以使用接口:

class Foo {
  ...

  private interface MyEnumConstants {
    static final String MY_STRING = "a string I use in a constructor";
    static final String MY_OTHER_STRING = "a string I use in another constructor";      
  }

  public enum MyEnum implements MyEnumConstants {
    MyEnumType(1, MY_STRING),
    MyEnumType2(2, MY_STRING),
    MyEnumType3(3, MY_OTHER_STRING);

    MyEnum(int num, String str) {
      ...
    } 
  }
 ...
}

您可以像這樣在枚舉中聲明接口

enum MyEnum {
    MyEnumType(1, EnumConstants.MY_STRING), 
    MyEnumType2(2, EnumConstants.MY_STRING), 
    MyEnumType3(3, EnumConstants.MY_OTHER_STRING);

    MyEnum(int num, String str) {

    }

    interface EnumConstants {
        static final String MY_STRING = "a string I use in a constructor";
        static final String MY_OTHER_STRING = "a string I use in another constructor";
    }
}

對前面答案的反對意見是1)有些不起作用,2)有些沒有真正的常量,3)有些需要嵌入式或額外的類。 這樣做怎么樣?

public class Constants {
    public static final double EQUATORIALRADIUS = 6378137.0;
}

public enum Planet {
    EARTH (Constants.EQUATORIALRADIUS),

“但是!”,你說,“還有一個額外的課程!” 不,你只需在編譯之后和dexing之前刪除Constants.class文件或者壓縮到jar。 或者你可以Proguard它。 在任何情況下,您都可以使用apktool驗證Constants類的任何位置都沒有引用。 所有常量都是真正的常量,並與smali內聯。

暫無
暫無

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

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