• java枚举类


     自己写的枚举类

    package com.yanan.java;
    
    /**
     * Created by zhangyanana    on 2017/2/28.
     */
    public class TestSeason {
        public static void main(String[] args) {
            Season season=Season.AUTUMN;
            System.out.println(season.getSeasonName()+"  "+season.getSeasonDesc());
        
        }
    }
    
    //自定义枚举类(类似于单例模式)
    class Season {
    
        //1.提供类的属性,声明为private final-----外部无法修改属性且属性值一旦被赋值就不可改变。
        private final String seasonName;
        private final String seasonDesc;
    
        //2.私有化构造器,让外部无法创建这个类的对象
        private Season(String seasonName, String seasonDesc) {
            this.seasonName = seasonName;
            this.seasonDesc = seasonDesc;
        }
    
        //3.通过公用的方法来调用属性
        public String getSeasonDesc() {
            return seasonDesc;
        }
    
        public String getSeasonName() {
            return seasonName;
        }
    
        //4.在类的内部创建类的对象(枚举类对象),设置为public static final
        public static final Season SPRING = new Season("spring", "春暖花开");
        public static final Season SUMMER = new Season("summer", "夏日炎炎");
        public static final Season AUTUMN = new Season("autumn", "秋高气爽");
        public static final Season WINTER = new Season("winter", "冬雷滚滚");
    
    }

    执行结果:

  • 相关阅读:
    seaborn可视化NOTE
    快速入门pandas
    protege下载安装使用
    用上Latex实现编辑伪代码
    决策树可视化
    关于时间
    地理三维模型制作
    Python使用记录
    编码格式
    素数生成算法小结
  • 原文地址:https://www.cnblogs.com/zhyn-BeHard/p/6482745.html
Copyright © 2020-2023  润新知