如何在静态枚举中获得下一个

时间:2014-08-15 09:09:11

标签: java static enums next

public enum Planet { 
    MERCURY(false),
    VENUS(false),
    EARTH(false),
    MARS(false),
    JUPITER(false),
    SATURN(false),
    URANUS(false),
    NEPTUNE(false); 
}

    public boolean isCurrent;

    Planet(boolean isCurrent){
       this.isCurrent = isCurrent;
    }

    public static void next(){
    if(planet == VENUS){
        VENUS.isCurrent = false;
        EARTH.isCurrent = true;
        MARS.isCurrent = false;
        JUPITER.isCurrent = false;
        SATURN.isCurrent = false;
        URANUS.isCurrent = false;
        NEPTUNE.isCurrent = false;
    }
    if(planet == EARTH){
        VENUS.isCurrent = false;
        EARTH.isCurrent = false;
        MARS.isCurrent = true;
        JUPITER.isCurrent = false;
        SATURN.isCurrent = false;
        URANUS.isCurrent = false;
        NEPTUNE.isCurrent = false;
    }
...  
}

我找到了这个解决方案,

private enum Planet { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE;
    public Planet getNext() {
        return this.ordinal() < Planet.values().length - 1
            ? Planet.values()[this.ordinal() + 1]
            : null;
    }
}

但是我无法使用这个因为这个枚举在其他类中作为静态导入;

现在我使用以下结构:

 public static Planet setNewCurrent(){
    for(Planet planet : Planet.values()){
        if(planet == VENUS){
            VENUS.isCurrent = false;
            EARTH.isCurrent = true;
            MARS.isCurrent = false;
            JUPITER.isCurrent = false;
            SATURN.isCurrent = false;
            URANUS.isCurrent = false;
            NEPTUNE.isCurrent = false;
        }
        if(planet == EARTH){
            VENUS.isCurrent = false;
            EARTH.isCurrent = false;
            MARS.isCurrent = true;
            JUPITER.isCurrent = false;
            SATURN.isCurrent = false;
            URANUS.isCurrent = false;
            NEPTUNE.isCurrent = false;
        }
...
    }
}   

有没有人知道像这样的getNextPlanet()方便的方法

planet.getNext().isCurrent = true;

3 个答案:

答案 0 :(得分:2)

这不是使用枚举的正确方法。你不应该在每个枚举常量上有boolean isCurrent

相反,在一个地方(不在枚举上)有一个Planet currentPlanet变量:

Planet currentPlanet = Planet.MERCURY;

当你想要获得下一个时:

currentPlanet = currentPlanet.getNext();

答案 1 :(得分:2)

我希望你能意识到你可以拥有以下内容:

public enum Planet { 
    MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE; 

    public Planet getNext() {
        return this.ordinal() < Planet.values().length - 1
            ? Planet.values()[this.ordinal() + 1]
            : null;
    }
}

private Planet planet;

public void someFunction()
{
    planet = Planet.MARS;
    planet = planet.getNext();
    if(planet != null)
    {
        doStuff();
    }
}

当然,您可以使用带有枚举的switch-case语句,该语句基于字段变量中选择的任何一个:Java using enum with switch statement

一个布尔变量,用于确定哪一个是&#34;当前被选中&#34;根本没有必要。

编辑:根据您的评论,您需要一个Planet的单例实例。幸运的是,你可以使用enum本身:

public enum Planet { 
    MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE; 

    private static Planet currentPlanet = MERCURY;

    public static Planet getCurrentPlanet()
    {
        return currentPlanet;
    }

    public static boolean setToNext() {
        boolean retVal = this.ordinal() < Planet.values().length - 1;
        currentPlanet = this.ordinal() < Planet.values().length - 1
                ? Planet.values()[this.ordinal() + 1]
                : Planet.values()[0];
        return retVal; //returns false when it was the last element of the enum
    }
}


public void doSomething()
{
    Planet planet;
    do
    {
        planet = Planet.getCurrentPlanet();
        //do things with planet, like write out their names
        System.out.println(planet.name());
    }
    while(Planet.setToNext());
}

答案 2 :(得分:0)

    public enum Planet {
        EARTH,
        MARS;

        public static Planet current = EARTH;

        Planet() {
        }

        public Planet getNext() {
            return values()[(ordinal() + 1) % values().length];
        }

        public static void setNext(){
            current = current.getNext();
        }

        public static Planet getCurrent(){
            return current;
        }


    }

import static com.test.Planet.*;

    public class Main {
        public static void main(String[] args){
            getCurrent();
            setNext();
        }
    }