如何使用 Java 枚举模拟多层菜单系统?

发布于 2024-09-29 00:28:48 字数 736 浏览 4 评论 0原文

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

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。

这有可能吗?除了枚举之外,您还有什么建议?

I need to accomplish the following (this is a simplified version):

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;
 }
}

So that I can access these items as follows:

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

The reason why I am attempting to do this with enums is the fact that I need to be able to access each tier without having to a) instantiate a class, b) hide the names of the tiers behind a method name, or c) use an iterator to go through an EnumSet.

Is this at all possible? What would you suggest instead of enums?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟凡古楼 2024-10-06 00:28:48
//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);
    }
}
//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);
    }
}
猫性小仙女 2024-10-06 00:28:48

以下是我最终实现解决方案的方式:

    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;

Here's how I finally ended up implementing my solution:

    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;
        }
      }

This way, I don't have to instantiate the class or call any class specific methods to get my desired info. I can get at all the "some value" strings like this:

string cat1 = Animals.Cats.tabby.getSomeValue;
string dog1 = Animals.Dogs.dachsund.getSomeValue;
string bird1 = Animals.Birds.canary.getSomeValue;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文