簡體   English   中英

如何在Enum方法中將Enum的值與Enum的所有可能值進行比較,並避免丟失return語句?

[英]How do I compare the value of an Enum with all possible values of the Enum in an Enum method and avoid missing return statement?

我只是在學習Java枚舉。 當我在下面運行代碼時,我得到一個錯誤,下面也會重現。 基本上,我的問題是:當我在枚舉中定義一個方法時,我想在該方法中檢查枚舉的值,以便可以基於該值執行某些操作,如何執行此檢查? 下面,我有一個帶有三個可能值的Enum,在方法getNext ,我有三個if語句,將這個Enum的值與三個可能值中的每一個進行比較。 但是我仍然收到一條錯誤消息,說存在一條沒有回報的道路。

package enumerations;

enum TrafficLightColor2 {
    RED(12), GREEN(10), YELLOW(2);

    private int waitTime;

    TrafficLightColor2(int waitTime) {
        this.waitTime = waitTime;
    }

    int getWaitTime() {
        return waitTime;
    }

    TrafficLightColor2 getNext() {
        if (this.equals(TrafficLightColor2.GREEN)) {
            return TrafficLightColor2.YELLOW;
        }
        if (this.equals(TrafficLightColor2.YELLOW)) {
            return TrafficLightColor2.RED;
        }
        if (this.equals(TrafficLightColor2.RED)) {
            return TrafficLightColor2.GREEN;
        }
    }
}

// A computerized traffic light.
class TrafficLightSimulator2 implements Runnable {
    private Thread thrd; // holds the thread that runs the simulation
    private TrafficLightColor2 tlc; // holds the traffic light color
    boolean stop = false; // set to true to stop the simulation
    boolean changed = false; // true when the light has changed

    TrafficLightSimulator2(TrafficLightColor2 init) {
        tlc = init;
        thrd = new Thread(this);
        thrd.start();
    }

    TrafficLightSimulator2() {
        tlc = TrafficLightColor2.RED;
        thrd = new Thread(this);
        thrd.start();
    }

    // Start up the light.
    public void run() {
        while (!stop) {
            try {
                Thread.sleep(tlc.getWaitTime());
            } catch (InterruptedException exc) {
                System.out.println(exc);
            }
            changeColor();
        }
    }

    // Change color.
    synchronized void changeColor() {
        tlc = tlc.getNext();
        changed = true;
        notify(); // signal that the light has changed
    }

    // Wait until a light change occurs.
    synchronized void waitForChange() {
        try {
            while (!changed)
                wait(); // wait for light to change
            changed = false;
        } catch (InterruptedException exc) {
            System.out.println(exc);
        }
    }

    // Return current color.
    synchronized TrafficLightColor2 getColor() {
        return tlc;
    }

    // Stop the traffic light.
    synchronized void cancel() {
        stop = true;
    }
}


class TrafficLightDemo2 {
    public static void main(String args[]) {
        TrafficLightSimulator tl =
                new TrafficLightSimulator(TrafficLightColor.GREEN);
        for (int i = 0; i < 9; i++) {
            System.out.println(tl.getColor());
            tl.waitForChange();
        }

        tl.cancel();
    }
}

我得到錯誤

$ javac enumerations/TrafficLightDemo2.java
enumerations/TrafficLightDemo2.java:26: error: missing return statement
    }
    ^
1 error
TrafficLightColor2 getNext() {
    if (this.equals(TrafficLightColor2.GREEN)) {
        return TrafficLightColor2.YELLOW;
    }
    if (this.equals(TrafficLightColor2.YELLOW)) {
        return TrafficLightColor2.RED;
    }
    if (this.equals(TrafficLightColor2.RED)) {
        return TrafficLightColor2.GREEN;
    }
}

如果所有3個if均為false,則此方法不返回值。

在處添加return或更好地拋出錯誤,例如

throw new IllegalArgumentException("Unsupported enum")

在枚舉類中使用實例字段的優點是,您可以輕松地將實現詳細信息與獨立於API的常量相關聯。 換句話說,您可以輕松地將數據與枚舉常量相關聯,這樣就可以接受一種優雅的解決方案,例如在您需要添加新的枚舉常量的情況下,您並不會永遠結婚。

因此,您可以在履行以下相同合同的同時極大地簡化實現:

enum TrafficLightColor2 {
    RED(2, 12), 
    GREEN(0, 10), 
    YELLOW(1, 2);


    private int order;   // implementation detail; non-exported
    private int waitTime;

    TrafficLightColor2(int ord, int waitTime) {
        this.order = ord;
        this.waitTime = waitTime;
    }

    int getWaitTime() {
        return waitTime;
    }

    TrafficLightColor2 getNext() {
        final int nextColor = (this.order + 1) % 3;  // magic numbers introduce fragility
        return Arrays.stream(TrafficLight2.values())
                .filter(e -> e.order == nextColor)
                .findAny()
                .get();
    }
}

這個版本對您的原始實現有一些好處:易於維護,因為如果添加了枚舉常量,則編譯器將強制您添加訂單值。 在原始版本中,如果您在添加常量后忘記修改if-else-block,則程序將繼續運行,但不會提供正確的行為。 而且由於order的實現是隱藏的,您可以隨時將其刪除或將其更改為其他實現,而不會影響API的正確性。

您是否考慮過將下一個狀態與聲明的值一起包含?

public enum TrafficLightColor2 {
    RED(12, "GREEN"), GREEN(10, "YELLOW"), YELLOW(2, "RED");

    int waitTime;
    String nextState;
    Configurations(int waitTime, String nextState) {
        this.waitTime = waitTime;
        this.nextState = nextState;
    }
    public int getWaitTime() {
        return waitTime;
    }
    public String getNextState() {
        return nextState;
    }
}

有了這個,你可以得到下一個狀態

TrafficLightColor2 trafficLightColor = TrafficLightColor2.GREEN;
System.out.println(TrafficLightColor2.valueOf(trafficLightColor.getNextState()));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM