枚举其实就是常量的规范应用:
定义season常量:
package com.enumTest;
public enum Season
{
SPRING(123),SUMMER(555);//定义对应的数字
private Integer value;
Season(Integer value)
{
this.value = value;
}
public Integer getValue() {
return value;//得到对应的数字
}
}
定义区域常量:
public enum Area
{
PROVINCE("广州",1);//必须全部大写
private String name;
private Integer id;
Area(String name,Integer id)
{
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public Integer getId() {
return id;
}
}
类中调用常量:
public class Demo
{
public static void main(String args[])
{
System.out.println(Season.SPRING.getValue());
System.out.println(Area.PROVINCE.getName());
}
}
网友评论