如何实现让一个类有固定个数的对象
手动封装构造方法(private) → 创建静态对象 → final修饰静态对象,使其成为常量
class Season { //枚举类
public final static Season SPRING = new Season();
public final static Season SUMMER = new Season();
public final static Season SUTUMN = new Season();
public final static Season WINTER = new Season();
private Season() {}
public void show() {
System.out.println("hello");
}
}
class Demo7 {
public static void main(String[] args) {
Season chunji = Season.SPRING;
chunji.show();
}
}
enum
从 JDK1.5 开始
enum Season {
SPRING,SUMMER,AUTUMN,WINTER //枚举项,枚举常量
}
class Demo7{
public static void main(String[] args) {
Season chunji = Season.SPRING;
chunji.show();
}
}
反编译后,跟上一个步骤类似
枚举类的本质就是一个类,被final修饰了,不能有子类,继承了java.lang.Enum
枚举类方法
-
int ordinal()
返回枚举项的编号,每个枚举项都有一个编号,编号从0开始enum Week{ MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY } class Demo10{ public static void main(String[] args){ Week[] weeks={Week.MONDAY,Week.TUESDAY,Week.WEDNESDAY,Week.THURSDAY,Week.FRIDAY,Week.SATURDAY,Week.SUNDAY}; System.out.println(weeks[0].ordinal());//0 System.out.println(weeks[2].ordinal());//2 System.out.println(weeks[6].ordinal());//6
-
int compareTo(E o)
比较枚举项的大小,比较的是编号System.out.println(weeks[6].compareTo(weeks[2]));//4 System.out.println(weeks[2].compareTo(weeks[4]));//-2 System.out.println(weeks[2].compareTo(weeks[2]));//0
-
String name()
返回枚举常量的名称,与其枚举声明中声明的完全相同System.out.println(weeks[6].name());//SUNDAY System.out.println(weeks[4].name());//FRIDAY
-
String toString()
返回声明中包含的此枚举常量的名称System.out.println(weeks[6].toString());//SUNDAY System.out.println(weeks[4].toString());//FRIDAY
-
static <T extends Enum> T value0f(Class enumType, String name)
返回具有指定名称的指定枚举类型的枚举常量Week obj = Enum.valueOf(Week.class,"MONDAY"); //MONDAY
-
public static Week[] values()
得到所有枚举项的数组Week[] arr = Week.values(); for(Week wk:arr) System.out.println(wk);//MONDAY TUESDAY WEDNESDAY ……
-
public static Week valueOf(java.lang.String);
返回具有指定名称的指定枚举类型的枚举常量Week xingqi = Week.valueOf("MONDAY"); System.out.println(xingqi);//MONDAY
定义属性
enum中构造方法默认被private修饰
enum Season{
SPRING("春季"),SUMMER("夏季"),AUTUMN("秋季"),WINTER("冬季");
private String desc;
Season(){} //默认就是private
Season(String desc){//默认就是private
this.desc=desc;
}
public String getDesc(){
return desc;
}
public String toString(){
return desc;
}
}
class Demo11{
public static void main(String[] args){
Season ss = Season.SUMMER;
System.out.println(ss);
}
}
定义方法
静态、非静态方法
enum Season{
……………………
public static void show()
{
System.out.println("Hello");
}
public void fun()
{
System.out.println("fun");
}
}
class Demo12
{
public static void main(String[] args)
{
Season xiaji=Season.SUMMER;
xiaji.show();
xiaji.fun();
}
}
抽象方法
含有抽象方法前的枚举类前不用写abstract,在每个枚举项里都必须重写抽象方法
enum Season{
SPRING("春季"){
public void show(){
System.out.println("春季");
}
},SUMMER("夏季"){
public void show(){
System.out.println("夏季");
}
},AUTUMN("秋季"){
public void show(){
System.out.println("秋季");
}
},WINTER("冬季"){
public void show(){
System.out.println("冬季");
}
};
……………………
public abstract void show();
}
class Demo13
{
public static void main(String[] args){
Season.WINTER.show();
}
}
实现接口
interface inter{
void ff();
}
enum Season implements inter{
SPRING("春季"),SUMMER("夏季"),AUTUMN("秋季"),WINTER("冬季");
………………
public void ff(){
System.out.println("ok");
}
}
class Demo14{
public static void main(String[] args){
Season.AUTUMN.ff();
Enum jijie=Season.WINTER; //多态
Object obj=Season.WINTER;
}
}
练习
定义一个Person类,有姓名和性别属性,性别定义成枚举
enum Gender{
MALE("男"),FEMALE("女");
private String desc;
Gender(){}
Gender(String desc){
this.desc=desc;
}
public String toString(){
return desc;
}
}
class Person{
private String name;
private Gender sex;
Person(){}
Person(String name,Gender sex){
this.name=name;
this.sex=sex;
}
public String toString(){
return name+","+sex;
}
}
class Demo15 {
public static void main(String[] args) {
Person ren=new Person("赵四",Gender.MALE);
System.out.println(ren);//赵四,男
}
}