0

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

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

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

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

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

4

2 回答 2

1
    enum Foods{  
      drinks, eats;     

     enum Drinks {   
        apple_juice, cola;  

      }  

      enum Eats{   
          potatoe, rice;  

    } 


} 

尝试打印:Foods.Eats.rice

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

于 2013-10-03T19:04:30.070 回答
0

最终解决方案代码(进行了一些简化以保持清晰)以及使用它的说明(查看 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);
    }
}
于 2013-10-04T15:54:02.567 回答