Java中的嵌套枚举?

前端之家收集整理的这篇文章主要介绍了Java中的嵌套枚举?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想为我的Web应用程序中提供的各种ajax服务定义一些枚举,例如:
Enum Service
{
    REGISTER,LOGIN,NEWS,FAQ
}

但是,这些枚举中的每一个都会有一个状态,如Failed,Loaded等.所以我希望能够使用REGISTER.LOADED,LOGIN.LOADED等来激活我的事件总线上的事件.但是每个状态枚举必须是唯一的. I.e Register.LOADED必须与FAQ.LOADED不同,依此类推.

编辑:此外,我必须能够将所有状态存储在同一个散列映射中,例如Register.LOADED和Login.LOADED必须可以存储在同一个散列映射中.并且父服务枚举,即logo,REGISTER等也必须存储在同一个散列映射中.

实现这一目标的最佳方法是什么?

解决方法

在OP的一些评论之后,我更新了我的代码.使用语法变得丑陋,在切换块中可能无法正常工作,但我相信这可以实现OP的目标:
import java.util.*;

public class HelloWorld
{
    public static void main(String []args)
    {
        // Different services are not equal
        System.out.println(Service.REGISTER + "==" + Service.LOGIN + " :: " + Service.REGISTER.equals(Service.LOGIN));
        // Same service is equal
        System.out.println(Service.REGISTER + "==" + Service.REGISTER + " :: " + Service.REGISTER.equals(Service.REGISTER));
        // Even after changing the state
        Service tmp = Service.REGISTER;
        Service.REGISTER.setState(Service.ServiceState.State.Failed);
        System.out.println(Service.REGISTER + "==" + tmp + " :: " + Service.REGISTER.equals(tmp));

        Service.REGISTER.setState(Service.ServiceState.State.LOADED);

        // Different service,same state is not equal
        System.out.println(Service.REGISTER + "." + Service.REGISTER.state + "==" + Service.LOGIN + "." + Service.LOGIN.state + " :: " + Service.REGISTER.state.equals(Service.LOGIN.state));

        // Same service,same state is equal (even when changing state around)
        Service.ServiceState.State temp = Service.REGISTER.getState();
        Service.REGISTER.setState(Service.ServiceState.State.LOADED);
        System.out.println(Service.REGISTER + "." + Service.REGISTER.state + "==" + tmp + "." + temp + " :: " + temp.equals(Service.REGISTER.getState()));

        // Same service,different state is not equal
        Service.REGISTER.setState(Service.ServiceState.State.Failed);
        System.out.println(Service.REGISTER + "." + Service.REGISTER.state + "==" + tmp + "." + temp + " :: " + temp.equals(Service.REGISTER.getState()));

        // Both service and state can be used as map keys
        Map<Service,String> map = new HashMap<Service,String>();
        Map<Service.ServiceState.State,String> map2 = new HashMap<Service.ServiceState.State,String>();
    }
}

enum Service
{
    REGISTER(),LOGIN(),NEWS(),FAQ();

    public ServiceState state;

    Service()
    {
        this.state = new ServiceState();
    }

    public void setState(ServiceState.State s)
    {
        state.state = s;
    }

    public ServiceState.State getState() { return state.state; }

    public static class ServiceState
    {
        public enum State
        {
            LOADED,Failed
        }

        public State state = State.LOADED;

        public ServiceState(){}

        public String toString() { return state.toString(); }

        public int hashCode()
        {
            return state.hashCode();
        }

        public boolean equals(Object obj)
        {
            return state.equals(obj);
        }
    }
}

输出

REGISTER==LOGIN :: false
REGISTER==REGISTER :: true
REGISTER==REGISTER :: true
REGISTER.LOADED==LOGIN.LOADED :: false
REGISTER.LOADED==REGISTER.LOADED :: true
REGISTER.Failed==REGISTER.LOADED :: false

原答案:

使用您想要的语法不太可能,但Java枚举基本上只是另一个名称的类. (事实上​​,一旦编译,它们就是扩展java.lang.Enum< E extends Enum< E>>)的类.

public class HelloWorld
{
    public static void main(String []args)
    {
        Service a = Service.REGISTER;
        a.state = Service.ServiceState.LOADED;
        System.out.println(a);
    }
}

enum Service
{
    REGISTER(),FAQ();

    public ServiceState state;

    Service()
    {
        this.state = ServiceState.LOADED;
    }

    public String toString()
    {
        return super.toString() + "." + state.toString();
    }

    public enum ServiceState
    {
        LOADED,Failed
    }
}

输出

REGISTER.LOADED

猜你在找的Java相关文章