JAVA代码 public enum EnumTest { HELLO,WORLD } 字节码 public final class EnumTest extends java.lang.Enum<EnumTest> { public static final EnumTest HELLO; public static final EnumTest WORLD; public static EnumTest[] values(); Code: 0: getstatic #1 // Field…
定义义一个交通灯枚举类,包含红灯.绿灯.黄灯,需要有获得下一个灯的方法,并实现红灯出现5秒之后变成绿灯,绿灯3秒之后变成黄灯,黄灯2秒之后变成红灯,如此循环 public class Test5 { /** * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { Deng[] deng = Deng. values(); for( int i =…
30:用enum代替int常量 当需要一组固定常量的时候,应该使用enum代替int常量,除了对于手机登资源有限的设备应该酌情考虑enum的性能弱势之外. 31:用实例域代替序数 应该给enum添加int域,而不是使用ordinal方法来导出与枚举关联的序数值.(几乎不应使用ordinal方法,除非在编写像EnumMap这样的基于枚举的通用数据结构) //WRONG public enum Fruit{ APPLE, PEAR, ORANGE; public int numberOfFruit(…
枚举 scala不用关注枚举的特别语法,取而代之的是标准库中的类, scala.Enumeration 想要创建新的枚举,只需要拓展这个类的对象即可 object Color extends Enumeration{ val Red = Value val Green = Value val Blue = Value } object Test3{ def main(args:Array[String]):Unit={ for (dir <- 0 to Direction2.maxId-1){…