gpt4 book ai didi

java - 枚举持续时间问题

转载 作者:行者123 更新时间:2023-12-02 00:45:08 25 4
gpt4 key购买 nike

所以如果我们有一个像这样的枚举:

  public enum light { red, yellow, green }

持续时间是多长?不是光是某种颜色的时候吗?

喜欢

int duration = ligt.red = 1

或者类似的东西?

最佳答案

像这样声明你的枚举:

public enum Light { 
RED(1),
YELLOW(2),
GREEN(3);

private final int duration;

Light(int duration) {
this.duration = duration;
}

public int getDuration() {
return this.duration;
}
}

然后你可以像这样使用它:

public class Test {
public static void main(String... args) {
Light light = Light.RED;
System.out.println("Duration of RED is: " + light.getDuration());
}
}

编辑:根据 Steve Kuo 的建议,变量 duration 已最终确定。

关于java - 枚举持续时间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5226282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com