看张老师的视频,感觉一个奇怪的问题,如下
- package cn.itcast.day1;
- import java.util.Date;
- public class EnumTest {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
-
- WeekDay1 weekDay = WeekDay1.MON;
- //System.out.println(weekDay.nextDay());
-
- WeekDay weekDay2 = WeekDay.FRI;
- System.out.println(weekDay2);
- System.out.println(weekDay2.name());
- System.out.println(weekDay2.ordinal());
- System.out.println(WeekDay.valueOf("SUN").toString());
- System.out.println(WeekDay.values().length);
-
- //new Date(300){};//匿名内部类
- TrafficLamp t = TrafficLamp.RED;
- // System.out.println(t);
- // System.out.println(t.nextLamp());
- }
- enum WeekDay{
- SUN(1),MON(),TUE,WED,THI,FRI,SAT;
- private WeekDay(){System.out.println("first");}
- private WeekDay(int day){System.out.println("second");}
- }
-
- public enum TrafficLamp{
- RED(30){
- public TrafficLamp nextLamp(){
- return GREEN;
- }
- },
- GREEN(45){
- public TrafficLamp nextLamp(){
- return YELLOW;
- }
- },
- YELLOW(5){
- public TrafficLamp nextLamp(){
- return RED;
- }
- };
- public abstract TrafficLamp nextLamp();
- private int time;
- private TrafficLamp(int time){this.time = time;}
- }
- }
复制代码 张老师说枚举也是一个特殊的类,上面的代码定义了2个枚举,而且全部是在EnumTest这个类“里面的”。难道这个2个枚举属于内部类吗?
并且,如果在一个类中,是不能把多个类都设置为public,上面有一个枚举的权限修饰符是public也不报错(上面的代码全部是正确的)。不知道为什么不报错?
|