1.用枚举方法实现颜色的判断与输出。
package chy; public class TestJava { enum Color { RED, YELLOW, BLACK } public void showColor(Color c) { switch (c) { case RED: System.out.println("RED"); break; case YELLOW: System.out.println("YELLOW"); break; case BLACK: System.out.println("BLACK"); break; default: System.out.println("color"); break; } } public static void main(String[] args) { TestJava tj = new TestJava(); tj.showColor(Color.RED); } }
2.使用枚举方法中的构造方法。
package chy; public class TestJava { enum Color { RED("red", 1), YELLOW("yellow", 2), BLACK("black", 3); String color; int in; Color(String color, int i) { this.color = color; this.in = i; } public String getColor() { return color; } public int getIndex() { return in; } } public static void main(String[] args) { System.out.println(Color.RED.getColor() + Color.RED.getIndex()); } }