是否有可能(如果是的话如何)在 Java 中嵌套多个枚举



我知道可以使用 B 作为接口,最后 C 作为实现此接口的枚举成员,以A.B.C的方式声明 3 个级别的枚举。

但我想嵌套几个枚举来映射具有固定数量成员的常量树结构。当然,像Tree.A.Leaf.B.Node.C.Something.D或简单的A.B.C.D这样的东西看起来不错。可能吗?找不到任何实现它的方法。谢谢。

更新(生成的解决方案):

  • 枚举对这种情况真的很糟糕,谢谢大家说服我。
  • 最后,我基于具有私有构造函数和静态字段的静态类构建解决方案。

示例代码作为我自己的答案放置,以保持问题清晰。希望这会帮助其他人。

    enum Foods{  
      drinks, eats;     
     enum Drinks {   
        apple_juice, cola;  
      }  
      enum Eats{   
          potatoe, rice;  
    } 

} 

尝试打印:Foods.Eats.rice

但它看起来很糟糕,味道很糟糕!!

最终解决方案代码(有一些简化以保持清晰),并附有使用它的说明(查看 main() 方法的内部)。这就是我一直在寻找的。

package com.test;
/*
 * Umbrella class which defines whole DB schema structure and some of global operations.
 **/
public class Schema {
    /**
     * Schema elements are open for those who need access to them on appropriate level
     * (HBase API). It should be encapsulated by business logics as development progresses
     * but there's always something which is not covered so we keep them open.
     *
     * Schema elements are obviously something which gives
     * you access to its string name and name prepared to be used by HBase.
     */
    public interface NamedEntityInterface {
        /**
         * Just receive name as it is used by DB.
         * @return DB level name as string.
         */
        public abstract String getName();
        /**
         * @return DB level name to be used for HBase API calls.
         */
        public abstract byte[] getNameBytes();
    }
    /**
     * NamedEntity class provides most generic implementation for NamedEntityInterface
     * widely used through all the schema.
     */
    public abstract static class NamedEntity implements NamedEntityInterface {
        private final String name;
        private final byte[] nameBytes;
        private NamedEntity(String name) {
            this.name = name;
            this.nameBytes = name.getBytes();
        }
        @Override
        public String getName() {
            return name;
        }
        @Override
        public byte [] getNameBytes() {
            return nameBytes;
        }
    }
    /**
     * Column abstraction.
     */
    public static class Column extends NamedEntity {
        private Column(String name) {
            super(name);
        }
    }
    /**
     * Column family abstraction.
     */
    public static class Family extends NamedEntity {
        static final String NAME_DEFAULT = "d";
        private Family(String name) {
            super(name);
        }
    }
    /**
     * Table abstraction.
     */
    public static class Table extends NamedEntity {
        private Table(String name) {
            super(Schema.class.getPackage().getName() + '.' + name);
        }
    }
    public static class TableA extends Table {
        public static class FamilyDefault extends Family {
            private FamilyDefault() {
                super(Family.NAME_DEFAULT);
            }
            public static final Column a = new Column("b");
            public static final Column b = new Column("a");
        }
        private TableA() {
            super("table.A");
        }
        public static final FamilyDefault familyDefault = new FamilyDefault();
    }
    public static class TableB extends Table {
        public static class FamilyA extends Family {
            public static final Column a = new Column("a");
            public static final Column b = new Column("b");
            private FamilyA() {
                super(NAME_DEFAULT);
            }
        }
        public static class FamilyB extends Family {
            private FamilyB() {
                super("f");
            }
        }
        private TableB() {
            super("table.B");
        }
        public static final FamilyA familyA = new FamilyA();
        public static final FamilyB familyB = new FamilyB();
    }
    static public TableA tableA = new TableA();
    static public TableB tableB = new TableB();
    public static void main(String [] args) {
        String tableName = Schema.tableA.getName();
        Family someFamily = Schema.tableA.familyDefault;
        byte [] column = Schema.tableA.familyDefault.a.getNameBytes();
        String tableBFamilyBName = Schema.tableB.familyB.getName();
        System.out.println("name: " + tableBFamilyBName);
    }
}

最新更新