枚举的深入理解
编程技术  /  houtizong 发布于 3年前   70
package com.jnmc.Enum;public class Weekday {//构造方法必须私有//保证其他任何类都不能创建Weekday的实例private Weekday(){}private String value = null;//在静态初始化的时候创建对象//引用必须是共有的静态的public static Weekday SUN = getInstance("SUN");public static Weekday MON = getInstance("MON");public static Weekday TUE = getInstance("TUE");public static Weekday WED = getInstance("WED");public static Weekday THU = getInstance("THU");public static Weekday FRI = getInstance("FRI");public static Weekday SAT = getInstance("SAT");private static Weekday getInstance(String day){Weekday d = new Weekday();d.value = day;return d;}@Overridepublic String toString(){return value;}}
public enum Color{//创建Color实例时会默认调用无参的构造方法//RED(3)调用了有参数的构造方法//以下代码必须放在enum体的最开头(注释除外)RED(3),YELLOW,BLUE; //若之后还有内容,应为‘;’//注意构造方法只允许声明为私有的private Color(){System.out.println("constructor without parameters");}private Color(int i){System.out.println("constructor with one integer parameter");}}
public enum Weekday{SUN(1){//用匿名内部类创建实例@Overridepublic Weekday nextday() {return MON;///这里更进一步说明了,MON是Weekday类型的引用}},///两个枚举的取值之间用','MON{@Overridepublic Weekday nextday() {return TUE;}},TUE{@Overridepublic Weekday nextday() {return WED;}},WED{@Overridepublic Weekday nextday() {return THUR;}},THUR{@Overridepublic Weekday nextday() {return FRI;}},FRI{@Overridepublic Weekday nextday() {return SAT;}},SAT{@Overridepublic Weekday nextday() {return MON;}};///最后一个用‘;’///定义了一个抽象方法,//则在创建Weekday的时候就必须实现该抽象方法public abstract Weekday nextday();private Weekday(){}private Weekday(int i){}}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接