2010-10-22 69 views
2

我需要完成以下(這是一個簡化版本):如何使用Java枚舉模仿多層菜單系統?

enum Animals{ 
enum Cats{tabby("some value"), siamese("some value")}, 
enum Dogs{poodle("some value"), dachsund("some value")}, 
enum Birds{canary("some value"), parrot("some value")} 

private String someValue = ""; 

private ShopByCategory(String someValue) 
{ 
    this.someValue = someValue; 
} 

public String getSomeValue() 
{ 
    return this.someValue; 
} 
} 

所以,我可以通過以下方式訪問這些項目:

string cat1 = Animals.Cats.tabby.getSomeValue; 
string dog1 = Animals.Dogs.dachsund.getSomeValue; 
string bird1 = Animals.Birds.canary.getSomeValue; 

爲什麼我試圖用這樣做的原因枚舉是這樣一個事實,即我需要能夠訪問每個層而不必實例化一個類,b)隱藏方法名稱後面的層的名稱,或者c)使用迭代器來檢查EnumSet。

這是可能的嗎?你會建議什麼,而不是枚舉?

回答

1
//Animals.java 
public class Animals { 
    public static class Cats { 
     public static final String tabby = "some value"; 
     public static final String siamese = "some value"; 
    } 
    public static class Dogs { 
     public static final String poodle = "some value"; 
     public static final String dachsund = "some value"; 
    } 
    public static class Birds { 
     public static final String canary = "some value"; 
     public static final String parrot = "some value"; 
    } 
} 

//ShopByCategory.java 
public class ShopByCategory { 
    private String someValue; 
    public ShopByCategory(String value){ 
     this.someValue = value; 
    } 
    public String getSomeValue(){ 
     return this.someValue; 
    } 
} 
//Main.java - an example of what you can do 
public class Main { 
    public static void main(String[] args){ 
     ShopByCategory sbc = new ShopByCategory(Animals.Birds.canary); 
     System.out.println(sbc.getSomeValue()); 
     System.out.println(Animals.Dogs.poodle); 
    } 
} 
+0

有趣的是看到pre java 1.5代碼。爲什麼這樣做時,枚舉是如此容易,更不用說類型安全? – Erik 2011-02-07 20:09:03

1

下面是我終於結束了實施我的解決方案:

public static class Animals() 
    { 
     public enum Cats() 
     { 
     tabby("some value"), 
     siamese("some value"); 

     private String someValue = ""; 

     private ShopByCategory(String someValue) 
     { 
      this.someValue = someValue; 
     } 

     public String getSomeValue() 
     { 
      return this.someValue; 
     } 
     } 

     public enum Dogs() 
     { 
     poodle("some value"), 
     dachsund("some value"); 

     private String someValue = ""; 

     private ShopByCategory(String someValue) 
     { 
      this.someValue = someValue; 
     } 

     public String getSomeValue() 
     { 
      return this.someValue; 
     } 
     } 

     public enum Birds() 
     { 
     canary("some value"), 
     parrot("some value"); 

     private String someValue = ""; 

     private ShopByCategory(String someValue) 
     { 
      this.someValue = someValue; 
     } 

     public String getSomeValue() 
     { 
      return this.someValue; 
     } 
     } 

這樣,我沒有實例化類或調用任何類特定的方法,讓我想要的信息。我可以得到所有這樣的「一些價值」字符串:

string cat1 = Animals.Cats.tabby.getSomeValue; 
string dog1 = Animals.Dogs.dachsund.getSomeValue; 
string bird1 = Animals.Birds.canary.getSomeValue;