2011-12-05 51 views
18

IM在java的工作,我已經創建了一個枚舉如下:帶有空格的Java枚舉元素?

public enum myEnum 
{ 
    india, 
    russian, 
    england, 
    north America 
} 

上面的例子,而在元素的名稱(如北美)使用空間給錯誤。 任何建議如何解決上述問題?

+1

對這個成員的訪問是不可能的;想想'myEnum x = myEnum.north America'。這些將不會是compiable,因爲如果允許這樣的名稱,在哪裏設置標識符的結尾?也許下一個代碼行或語句應該包含在內? – Matten

+1

不要使用小寫字母作爲枚舉,甚至不要使用類名! – banterCZ

回答

52

您不能在標識符的中間放置空格。

這樣做結束了,標識符和解析器假定接下來的任何內容都是該語句的上下文中的有效標記。有幾個(如果有的話)是合法的。

傳統的Java值名稱是:

INDIA,   // Or India, 
RUSSIA,  //  Russia, 
NORTH_AMERICA; //  NorthAmerica; 

enum可以有關聯的屬性,如人類可讀的名稱,例如,

public enum CountryAndOneContinent { 

    INDIA("India"), 
    RUSSIA("Russia"), 
    NORTH_AMERICA("North America"); 

    private String displayName; 

    CountryAndOneContinent(String displayName) { 
     this.displayName = displayName; 
    } 

    public String displayName() { return displayName; } 

    // Optionally and/or additionally, toString. 
    @Override public String toString() { return displayName; } 
} 

我很矛盾有關使用toString提供表示層表示。

我更喜歡方法明確溝通他們的目的–它更具表現力和明顯。

toString是非常通用的,只允許一個表示。取決於上下文,參數等,toString不允許使用多種輸出格式。

toString的優點包括對對象使用默認的字符串操作,在這種情況下,使用valueOf直接從人類可讀版本轉換爲枚舉值。

+2

您可能需要一個不同於名稱的名稱...(OP中只有一個元素是一個真實的國家,一個國家,一個語言,一個組成國家和一個大洲,相當於一個枚舉) –

+2

+1用toString()提到問題。你可以添加一個'getDisplayName()'方法,而不是使用'toString()'。 – Jesper

+0

@Jesper是的,我想爲'getXxx'專門設計的東西 - 我把它留給了JavaBeany。 –

3

把它們寫在一起,如northAmerica或使用下劃線north_America

+0

感謝都鐸王朝,但我不想像上面說的那樣做。需要保持這些詞分離。 – BSalunke

+3

@BSalunke你不能。不過,你可以創建一個帶有值的'enum'。查看更新。 –

+2

只是爲了強調戴夫牛頓所說的話:你不能,它不會那樣工作。 – Tudor

1
public enum myEnum 
{ 
    india, 
    russian, 
    england, 
    north_america 
} 

來訪問值

myEnum.values() 
+0

不要使用小寫字母作爲枚舉,甚至不要使用類名! – banterCZ

+0

@banterCZ sic erat scriptum –

2

Java naming rule不允許的變量,類,枚舉和枚舉成員(和所有其他標識符)的名稱白空間儘可能字符。因此這個「問題」無法解決。只需使用`north_america'作爲會員名稱即可!

An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.

The Java letters include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ character should be used only in mechanically generated source code or, rarely, to access preexisting names on legacy systems.

The "Java digits" include the ASCII digits 0-9 (\u0030-\u0039).

+0

這是一個語法錯誤,您可以在不遵循約定的情況下編寫可編譯的代碼。 –

+0

我不是英語母語的人,但對我的理解來說,它應該被稱爲約定:-) – Matten

+0

'public enum myEnum'在這裏,他沒有遵循約定。但是'北美'是一個語法錯誤,它不會被編譯。 –

4

該問題沒有(具體)與枚舉有關:在Java中,名稱不能有空格。嘗試消除空間(使用大寫來區分這些位)或使用下劃線。

14

我龔繼續猜想爲什麼你想要一個名字空間;因爲你想把它作爲一個字符串引用。

所以做到這一點:

public enum MyEnum { 
    INDIA("India"), 
    RUSSIAN("Russian"), 
    ENGLAND("England"), 
    NORTH_AMERICA("North America");  

    private String name; 

    MyEnum(String name) { 
     this.name = name; 
    } 

    public String getName() { 
     return this.name; 
    }  
} 

你可以考慮壓倒一切的字符串。

public toString() { 
    return name; 
} 

壓倒一切的toString的優點()是,該字符串也可在MyEnum .valueOf(myString的)一起使用。所以重寫toString基本上創建了枚舉值的HashMap。

+2

除非我在說些什麼,否則這個例子在Java 8中不起作用; 'MyEnum.valueOf(「North America」)'和'MyEnum.valueOf(MyEnum.NORTH_AMERICA.toString())'都會拋出相同的錯誤:'IllegalArgumentException無枚舉常量ofi.devops.cmwf.model.trigger.MyEnum.North America' – raffian

1

只是讓你自己的價值就像你的枚舉類中的函數。用下劃線替換空格。將這個枚舉常量命名爲「north_america(」North America「)」。如果該方法無法找到您的枚舉,它只是返回參數。

public static String valueOfOrDefault(String myValue) { 
//replace space with underscore so it matches enum name 
     String value=myValue.toUpperCase().replaceAll("\\s", "_"); 
     for(myEnum type : myEnum.class.getEnumConstants()) { 
      if(type.name().equalsIgnoreCase(value)) { 
      return type.toString(); 
      } 
     } 
     return myValue; 
     } 
0

我還介紹了一個自己的valueOf方法。但是我用Enum Type返回,我不必轉換「checkString」。例如,checkString是「Petty Cash」。

JournalType journalType = JournalType.valueOfOrDefault(checkString);

public enum JournalType { 

MISC_BILLING("Misc Billing"), 
MISC_BILLING_AUTONUM("Misc Billing AutoNum"), 
PRETTY_CASH("Petty Cash"), 
RECURRING_AP("Recurring AP"), 
GL_JOURNAL("GL Journal"), 
GDS_NDS("GDS/NDS"); 

private final String journalType; 

JournalType(String journalType) { 
    this.journalType = journalType; 
} 

@Override 
public String toString() { 
    return journalType; 
} 

public static JournalType valueOfOrDefault(String myValue) { 
    for(JournalType type : JournalType.class.getEnumConstants()) { 
     if(type.toString().equals(myValue)) { 
      return type; 
     } 
    } 
    throw new IllegalArgumentException("JournalType not found"); 
} 

}