Java枚举知识点

来源:互联网 发布:task host windows 10 编辑:程序博客网 时间:2024/05/30 22:57

一、定义:

一个类的对象是有限的,并且他的属性是固定的,比如季节类,他只有四个对象,春天、夏天、秋天、冬天,他们各自的属性也是固定的。


注意:枚举也是类。


二、手工定义枚举类(多例模式)

package com.lei.test;/** * 手工定义枚举类 * Author: pibigstar * Created on: 2017年8月27日 下午1:59:46 */public class Season {//他有两个属性(名字和描述)并且是固定的,不能被修改。public final String NAME;public final String DESC;//因为对象的个数是固定的,所以我们不能让外部创建对象,就将构造器私有化即可private Season(String nAME, String dESC) {NAME = nAME;DESC = dESC;}//季节只有四个对象,且不能被修改public static final Season spring = new Season("春天", "万物苏醒");public static final Season summer = new Season("夏天", "烈日炎炎");public static final Season autumn = new Season("秋天", "秋风气爽");public static final Season winter = new Season("冬天", "寒风瑟瑟");@Overridepublic String toString() {return "Season [NAME=" + NAME + ", DESC=" + DESC + "]";}}


二、 使用enum 关键字定义枚举类


package com.lei.test;public enum SeasonEnum implements Info{//枚举第一行,必须是其对象列表。。。每个对象用,隔开  以;结尾。声明的同时就要为其赋值Spring("春天","万物苏醒"),Summer("夏天","烈日炎炎"),Autumn("秋天","秋风气爽"),Winter("冬天","寒风瑟瑟");//两个属性且不能被修改(用final修饰),名称和描述public final String name;public final String desc;private SeasonEnum(String name, String desc) {this.name = name;this.desc = desc;}@Overridepublic String getInfo() {switch (this) {case Spring: return "我是春天";case Summer: return "我是夏天";case Autumn: return "我是秋天";case Winter: return "我是冬天";}return "没有此枚举";}}

接口Info

package com.lei.test;public interface Info {String getInfo();}


测试

package com.lei.test;public class Test {public static void main(String[] args) {//手工定义的枚举类使用Season season = Season.spring;//用枚举关键字(enum)定义的枚举使用SeasonEnum seasonEnum = SeasonEnum.Spring;//枚举里面的方法使用,values() 返回枚举里面所有枚举对象的数组SeasonEnum [] sEnums = SeasonEnum.values();for (SeasonEnum sEnum : sEnums) {System.out.println(sEnum);}/**输出结果 * SpringSummerAutumnWinter */// 通过一个字符串,返回跟此字符串相同的对象String spring = "Spring";SeasonEnum s = Enum.valueOf(SeasonEnum.class, spring);System.out.println(s);/**输出结果 * Spring *///实现接口的测试System.err.println(s.getInfo());/**输出结果 * 我是春天 */}}


再写一个方法枚举的例子


package com.lei.test;public enum Method {POST(1,"post"),GET(2,"get");private int id;private String method;private Method(int id,String method){this.id = id;this.method = method;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getMethod() {return method;}public void setMethod(String method) {this.method = method;}public static Method getByID(int id){switch (id) {case 1:return POST;case 2:return GET;default:throw new RuntimeException("没有此id对应的枚举");}}public static Method getByMethod(String method){switch (method) {case "post":case "POST":return POST;case "get":case "GET":return GET;default:throw new RuntimeException("没有此method对应的枚举");}}}

测试测枚举

package com.lei.test;public class EnumTest {public static void main(String[] args) {//输出枚举定义时其POST(1,"post")中的post值System.out.println(Method.POST.getMethod());//通过id获得一个枚举System.out.println(Method.getByID(1).getMethod());//遍历全部枚举for (Method method : Method.values()) {System.out.println(method);}}}