首页 > 编程语言 > 详细

java枚举学习笔记

时间:2018-08-12 21:24:57      阅读:160      评论:0      收藏:0      [点我收藏+]
枚举概述:
是将变量的值一一列举来,变量的值只限列举的值得范围内,比如:一周只有7天,一年只有12个月,一天24小时,一年60甲子;
单利模式:单利类是一个类的只有一个实例;

  1. 自动拆装箱
  2. 泛型
  3. 可变参数
  4. 静态导入
  5. 增强for循环
  6. 互斥锁
  7. 枚举

测试类

/**
     * @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();
}

java枚举学习笔记

原文:http://blog.51cto.com/357712148/2158323

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!