直接上代码
?
class="java" name="code">/** * * 〈枚举测试〉<br> * * 1 枚举构造 * 2 枚举实现接口 * 3 枚举使用 * */ public class EnumExample { public static void main(String[] args) { Signal[] s = Signal.YELLOW.values(); System.out.println("序号"); for (Signal ss : s) { System.out.print(ss.ordinal()+"\t"); } System.out.println();System.out.println("值"); for (Signal ss : s) { System.out.print(ss.getCode()+"\t"); } System.out.println();System.out.println("变量名"); for (Signal ss : s) { System.out.print(ss.name()+"\t"); } // 运行异常:不需要try throw new RuntimeException(""); } /** * code 接口 */ interface CodeFC { int getCode(); void setCode(int code); } /** * 枚举定义 */ enum Signal implements CodeFC { GREEN(1), YELLOW(2), RED(3); private int code; Signal(int i) { this.code = i; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } } }
?