【1】数学:枚举法:
1<x<4
2<y<8
求x+y=6
枚举法:一枚一枚的列举出来。前提:有限,确定
【2】在java中,类的对象是有限个,确定的。这个类我们可以定义为枚举类
举例:
星期:一二三四五六日
性别:那女
季节:春夏秋冬
【3】自定义枚举类:(JDK1.5之前)
package com.msb.enum01; import sun.management.Sensor; /** * 开发人:liu * 日期:16:01:37 * 描述:定义枚举类:季节 * 版本:1.0 */ public class Season { //属性: private final String seasonName;//季节的名字 private final String seasonDesc;//季节描述 //利用构造器对属性进行赋值操作: //构造器私有化,外界不能调用这个构造器,只能Season内部自己调用 private Season(String seasonName,String seasonDesc){ this.seasonName=seasonName; this.seasonDesc=seasonDesc; } //提供枚举类的有限的 确定的对象: 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("冬天","白雪皑皑"); //提供Get方法 public String getSeasonName() { return seasonName; } public String getSeasonDesc() { return seasonDesc; } //对toString进行重写 @Override public String toString() { return "Season{" + "seasonName='" + seasonName + '\'' + ", seasonDesc='" + seasonDesc + '\'' + '}'; } }
public class Test { //这是一个main方法:是程序的入口 public static void main(String[] args) { Season season=Season.SPRING; System.out.println(season); System.out.println(season.getSeasonDesc()); } }
标签:String,Season,seasonDesc,final,seasonName,枚举,引入,public From: https://www.cnblogs.com/jeldp/p/16808896.html