入口类
public class xuleiTest { public static void main(String[] args) { String a = xuleiEnum.ONE.toString(); String b = xuleiEnum.TWO.toString(); String c = xuleiEnum.Three.toString(); String d = xuleiEnum2.ONE.toString(); String e = xuleiEnum2.TWO.toString(); System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); System.out.println(e); } }
不带参数的枚举类
public enum xuleiEnum2 { ONE,TWO; private String name = "xulei"; private String age = "12"; @Override public String toString() { return this.name + " "+ this.age; } }
带参数的枚举类
public enum xuleiEnum { ONE("one","1"){ },TWO("tow","2"){ },Three("three","3"){ }; xuleiEnum(String name,String age){ this.age = age; this.name = name; } private String name = "xulei"; private String age = "12"; public String getName() { return name; } public void setName(String name) { this.name = name; } public void setAge(String age) { this.age = age; } public String getAge(){ return age; } @Override public String toString() { return this.name + " "+ this.age; } }
输出结果
one 1 tow 2 three 3 xulei 12 xulei 12