如何将Enum嵌套在Enum的值中

时间:2010-04-14 11:19:13

标签: java enums

我想知道Java中是否可以嵌套枚举。

我希望能够做到这一点:

有一个枚举由CAT和DOG组成的物种将授予我访问可用CAT和DOG品种的子枚举的权限。例如,我希望能够测试CAT或DOG,以及动物是PERSAN CAT还是PITBULL DOG。 CAT和DOG品种必须是不同的枚举,即CatBreeds枚举和DogBreeds枚举。

以下是我想使用的访问模式示例:

物种:

  • Species.CAT
  • Species.DOG

品种:

  • Species.CAT.breeds.PERSAN
  • Species.DOG.breeds.PITBULL

3 个答案:

答案 0 :(得分:3)

你继续做这件事会不会更好?猫和狗将成为物种的子类。 Persion将成为Cat的子类,而Pitbull将成为Dog的子类。

想要使用Enums试图实现什么目标?

答案 1 :(得分:0)

嵌套枚举的一种方法:

package nested2;
import java.util.*;
interface HasChildren {
    Set<Enum<? extends HasChildren>> children();
    Class parent();
    Enum<? extends HasChildren> p();
}
enum Citrus implements HasChildren {
    lemon, lime, orange;
    Enum<? extends HasChildren> p;
    Class parent = getDeclaringClass();
    Class children;
    public Enum<? extends HasChildren> p() {
        return p;
    }
    public Class parent() {
        return parent;
    }
    public Set<Enum<? extends HasChildren>> children() {
        return children != null ? EnumSet.allOf(children) : null;
    }
    enum Orange implements HasChildren {
        navel, valencia, blood;
        Enum p;
        Class parent = getDeclaringClass();
        Class children;
        public Enum<? extends HasChildren> p() {
            return p;
        }
        public Class<Enum<? extends HasChildren>> parent() {
            return parent;
        }
        public Set<Enum<? extends HasChildren>> children() {
            return children != null ? EnumSet.allOf(children) : null;
        }
        enum Navel implements HasChildren {
            washinton, lateLane, caraCaraPink;
            Enum<? extends HasChildren> p;
            Class parent = getDeclaringClass();
            Class children;
            public Enum<? extends HasChildren> p() {
                return p;
            }
            public Class parent() {
                return parent;
            }
            public Set<Enum<? extends HasChildren>> children() {
                return null;
            }
        }
        static {
            navel.children = Navel.class;
            for (Navel n : Navel.values())
                n.p = orange;
            navel.p = navel;
        }
        enum Blood implements HasChildren {
            moro, taroco;
            Enum<? extends HasChildren> p;
            Class parent = getDeclaringClass();
            Class children;
            public Enum<? extends HasChildren> p() {
                return p;
            }
            public Class parent() {
                return parent;
            }
            public Set<Enum<? extends HasChildren>> children() {
                return null;
            }
        }
        static {
            blood.children = Blood.class;
            for (Blood b : Blood.values())
                b.p = blood;
            blood.p = orange;
        }
    }
    static {
        for (Orange o : Orange.values())
            o.p = orange;
        orange.children = Orange.class;
    }
}
public class EnumTree {
    static void visit(Class<?> clazz) {
        Object[] enumConstants = clazz.getEnumConstants();
        if (enumConstants[0] instanceof HasChildren) for (Object o : enumConstants)
            visit((HasChildren) o, clazz.getName());
    }
    static void visit(HasChildren hasChildren, String prefix) {
        if (hasChildren instanceof Enum) {
            System.out.println(prefix + ' ' + hasChildren + " (" + hasChildren.parent() + ")" + " (" + hasChildren.p()
                    + " " + ((Enum) hasChildren).getDeclaringClass());
            if (hasChildren.children() != null) for (Object o : hasChildren.children())
                visit((HasChildren) o, prefix + ' ' + hasChildren);
        } else
            System.out.println("other " + hasChildren.getClass());
    }
    static <E extends Enum<E> & HasChildren> Set<E> foo() {
        return null;
    }
    public static void main(String[] args) {
        //looks like p is the right parent
        System.out.println(Citrus.Orange.Navel.washinton);
        System.out.println(Citrus.Orange.Navel.washinton.getClass().getDeclaredClasses());
        System.out.println(Citrus.Orange.Navel.washinton.getClass().getEnclosingClass());
        System.out.println(Citrus.Orange.Navel.washinton.getClass().getDeclaringClass());
        visit(Citrus.lemon, "");
        System.out.println("----------------------");
        visit(Citrus.orange, "");
        System.out.println("----------------------");
        visit(Citrus.class);
        System.out.println("----------------------");
    }
}

答案 2 :(得分:-1)

public enum A {

    a1, 
    a2;

    public enum B {
        b1, 
        b2
    }
}

依旧......