首页 > 代码库 > 带构造方法和抽象方法的枚举

带构造方法和抽象方法的枚举

package cn.itcast.day1;

public class Enum {
public static void main(String[] args) {
Weekday1 weekday = Weekday1.SAT;

System.out.println(weekday.nextDay());


System.out.println(Weekday.FRI);
System.out.println(Weekday.MON.name());
System.out.println(Weekday.MON.ordinal());

}

public enum Weekday{
//最简单的枚举
SUN,MON,TUS,WED,THU,FRI,SAT;//此处可以加 ;号,亦可以不加

//带构造方法的枚举

//当枚举带构造方法时,1 ,构造方法必须放在属性后面
// 2, 属性后面必须加 ;号

// 3, 构造方法必须是私有的
//无参的构造方法
private Weekday(){}
//有参的构造方法

private Weekday(int day){}

}


public enum TrifficLight{
//带有抽象方法的枚举
GREEN(30){
public TrifficLight nextLight(){
return YELLOW;
}
},
RED(15){
public TrifficLight nextLight(){
return GREEN;
}
},
YELLOW(5){
public TrifficLight nextLight(){
return RED;
}
};

//下一个灯为

public abstract TrifficLight nextLight();


//定义一个有参的构造方法

private int time;//每种灯亮的时间
private TrifficLight(int time){
this.time = time;
}
}

}