public class T2 { // Using the enumeration type declared by enum is equivalent to defining a class; this class inherits the class Enum by default public static void main(String[] args) { //Color.values(); returns all enumerated objects for (Color1 c : Color1.values()) { System.out.println(c.ordinal() + " " + c.getName()); } } } enum Color1 { RED("red"), BLUE("blue"), GREEN("green");//This constructor must be called when declaring an enumeration private String name; private Color1(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
public class T6 { //Let the enumeration class implement an interface public static void main(String[] args) { for(Color8 c : Color8.values()){ System.out.println(c.getColor()); } } } interface Print{ String getColor(); } enum Color8 implements Print{ RED{ public String getColor() { return "red"; } }, GREEN{ public String getColor() { return "green"; } }, BLUE{ public String getColor() { return "blue"; } };