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 に答える