2

我需要完成以下(这是一个简化版本):

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;

我尝试使用枚举执行此操作的原因是,我需要能够访问每一层,而不必 a) 实例化一个类,b) 将层的名称隐藏在方法名称后面,或者 c) 使用一个遍历 EnumSet 的迭代器。

这是可能吗?你会建议什么而不是枚举?

4

2 回答 2

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;
于 2011-02-07T19:13:45.533 回答
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);
    }
}
于 2010-10-22T19:45:07.610 回答