繁体   English   中英

是否可以在Java中嵌套多个枚举?

[英]Is it possible (and how if yes) to nest several enums in Java?

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

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

UPDATE(结果解决方案):

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

将示例代码作为我自己的答案,以使问题更清楚。 希望这会对其他人有所帮助。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM