Java枚举类
1.枚举类入门
Java枚举类是从Java5开始支持的。
枚举类的构造方法是用private修饰的。枚举类的所有实例必须在类的第一行列出,否则这个枚举类不会产生对象。
而且这些实例都是public static void.
每个枚举类都有一个values方法,该方法可以遍历枚举类的所有实例
public class Test {public static void tell(Season s){switch(s){case Spring:System.out.println(s+"春天");break;case Summer:System.out.println(s+"夏天");break;case Fall:System.out.println(s+"秋天");break;case Winter:System.out.println(s+"冬天");break;}}public static void main(String[] args) {for(Season s:Season.values()){System.out.println(s);}tell(Season.Fall);}}enum Season{Spring,Summer,Fall,Winter;}
2.枚举类的构造方法和对象
枚举类的构造方法是private,所以不能使用new创建对象
可以使用Enum.valueOf(Class c,String s);创建对象
public class Test {public static void main(String[] args) {Season s=Enum.valueOf(Season.class, "Summer");System.out.println(s);}}enum Season{Spring,Summer,Fall,Winter;}
一旦为枚举类定义了带参数的构造方法后,列举枚举类就必须对应的传入参数
public class Test {public static void main(String[] args) {Season s=Enum.valueOf(Season.class, "Summer");System.out.println(s.name);}}enum Season{Spring("春天"),Summer("夏天"),Fall("秋天"),Winter("冬天");public String name;private Season(String name){this.name=name;}}
3.枚举类实现接口
与普通类一样,枚举类实现接口,则必须实现接口中所有的方法
public class Test {public static void main(String[] args) {Season s=Enum.valueOf(Season.class, "Summer");System.out.println(s.name);s.printInfo();}}enum Season implements Infomation{Spring("春天"),Summer("夏天"),Fall("秋天"),Winter("冬天");public String name;private Season(String name){this.name=name;}@Overridepublic void printInfo() {System.out.println(name);}}interface Infomation{void printInfo();}
也可以使用匿名内部类的方法为每一个枚举类对象实现接口
public class Test {public static void main(String[] args) {Season s=Enum.valueOf(Season.class, "Summer");System.out.println(s.name);s.printInfo();}}enum Season implements Infomation{Spring("春天"){@Overridepublic void printInfo() {// TODO Auto-generated method stubSystem.out.println("春天温暖");}},Summer("夏天"){@Overridepublic void printInfo() {// TODO Auto-generated method stubSystem.out.println("夏天炎热");}},Fall("秋天"){@Overridepublic void printInfo() {// TODO Auto-generated method stubSystem.out.println("秋天凉爽");}},Winter("冬天"){@Overridepublic void printInfo() {// TODO Auto-generated method stubSystem.out.println("冬天寒冷");}};public String name;private Season(String name){this.name=name;}}interface Infomation{void printInfo();}
4.包含抽象方法的枚举类
包含抽象方法枚举类,要实例化对象的话,就得让每个对象实现抽象方法
public class Test {public static void main(String[] args) {Season s=Enum.valueOf(Season.class, "Summer");System.out.println(s.name);s.printInfo();s.whatToDo();}}enum Season implements Infomation{Spring("春天"){@Overridepublic void printInfo() {// TODO Auto-generated method stubSystem.out.println("春天温暖");}@Overridepublic void whatToDo() {// TODO Auto-generated method stubSystem.out.println("我去春游");}},Summer("夏天"){@Overridepublic void printInfo() {// TODO Auto-generated method stubSystem.out.println("夏天炎热");}@Overridepublic void whatToDo() {// TODO Auto-generated method stubSystem.out.println("我去游泳");}},Fall("秋天"){@Overridepublic void printInfo() {// TODO Auto-generated method stubSystem.out.println("秋天凉爽");}@Overridepublic void whatToDo() {// TODO Auto-generated method stubSystem.out.println("我去玩乒乓球");}},Winter("冬天"){@Overridepublic void printInfo() {// TODO Auto-generated method stubSystem.out.println("冬天寒冷");}@Overridepublic void whatToDo() {// TODO Auto-generated method stubSystem.out.println("我去赏雪观梅");}};public String name;private Season(String name){this.name=name;}public abstract void whatToDo();}interface Infomation{void printInfo();}