枚举概述:
是将变量的值一一列举来,变量的值只限列举的值得范围内,比如:一周只有7天,一年只有12个月,一天24小时,一年60甲子;
单利模式:单利类是一个类的只有一个实例;
自动拆装箱
泛型
可变参数
静态导入
增强for循环
互斥锁
枚举
测试类
/** * @param args */ public static void main(String[] args) { //demo1(); //demo2(); Week3 mon = Week3.MON; mon.show(); }
第一个
public static void demo1() { Week mon = Week.MON; Week tue = Week.TUE; Week wed = Week.WED; System.out.println(mon); } public class Week { public static final Week MON = new Week(); public static final Week TUE = new Week(); public static final Week WED = new Week(); private Week(){} //私有构造,不让其他类创建本类对象}
第二种
public static void demo2() { Week2 mon = Week2.MON; System.out.println(mon.getName()); }public class Week2 { public static final Week2 MON = new Week2("星期一"); public static final Week2 TUE = new Week2("星期二"); public static final Week2 WED = new Week2("星期三"); private String name; private Week2(String name){ this.name = name; } //私有构造,不让其他类创建本类对象 public String getName() { return name; }}
第三种
public abstract class Week3 { public static final Week3 MON = new Week3("星期一") { //匿名类 public void show() { System.out.println("星期一"); } }; public static final Week3 TUE = new Week3("星期二"){ public void show() { System.out.println("星期二"); } }; public static final Week3 WED = new Week3("星期三"){ public void show() { System.out.println("星期三"); } }; private String name; private Week3(String name){ this.name = name; } //私有构造,不让其他类创建本类对象 public String getName() { return name; } public abstract void show();}