Java中的枚举类

我们现在需要一个枚举出四季以及他们各自的特点,四季只有春夏秋冬这四个季节,而且特点固定,所以我们不需要用户去创建新的季节对象。

自定义枚举

package Learn;
public class Test {
    public static void main(String[] args) {
        System.out.println(Season.SPRING.toString());
        System.out.println(Season.SUMMER.toString());
        System.out.println(Season.AUTUMN.toString());
        System.out.println(Season.WINTER.toString());
    }
}
class Season{
    private String season, characteristic;
    /**将构造其私有化,防止在main方法中直接new对象*/
    private Season(String season,String characteristic){
        this.season=season;
        this.characteristic=characteristic;
    }
    /**定义四个季节的特点,使用static使得在main方法中不用创建实例对象,但是会造成类的加载,所以再加上final变成常量值,避免了类的加载*/
   public static final Season SPRING =new Season("春天","温暖");
   public static final Season SUMMER =new Season("夏天","炎热");
   public static final Season AUTUMN =new Season("秋天","凉爽");
   public static  final Season WINTER =new Season("冬天","寒冷");

    @Override
    public String toString(){
        return season+characteristic;
    }

}


  1.  不需要提供set方法,因为枚举对象值通常为只读。
  2. 对枚举对象属性使用final+static共同修饰,实现底层优化
  3. 枚举对象属于常量,通常全部大写

enum关键字枚举

将定义的常量对象定义在最前面

package Learn;
public class Test {
    public static void main(String[] args) {
        System.out.println(Season.SPRING.toString());
        System.out.println(Season.SUMMER.toString());
        System.out.println(Season.AUTUMN.toString());
        System.out.println(Season.WINTER.toString());
    }
}
enum Season{
    SPRING("春天","温暖"),
    SUMMER ("夏天","炎热"),
    AUTUMN ("秋天","凉爽"),
    WINTER ("冬天","寒冷");
    private String season, characteristic;
    /**将构造其私有化,防止在main方法中直接new对象*/
    private Season(String season,String characteristic){
        this.season=season;
        this.characteristic=characteristic;
    }


    @Override
    public String toString(){
        return season+characteristic;
    }

}

注意事项:

  • 当我们使用enum关键字时,默认会继承enum类,我们反编译一下文件,可以发现同时也是final类型
  •  如果使用无参构造器,则实参列表与小括号都可以省略。例如下面的OTHER
  • 这个也是正确的,默认调用的无参构造器。

枚举中常用的方法

上面我们说到,enum其实是继承了ENUM类的,所以也会继承其方法

方法名作用
name输出对象的名字
ordinal输出该对象在枚举中的位置(从0开始)
hascode输出对应的对象地址编号
value将枚举填入数组中
valueof到对应的对象中去寻找,如果找到了,返回该对象,否则为false
compareTo比较两个对象的枚举编号,是他们顺序号的差值,可以crtl+B查看源码
package Learn;
public class Test {
    public static void main(String[] args) {
        System.out.println(Season.SPRING.toString());
        System.out.println(Season.SUMMER.toString());
        System.out.println(Season.AUTUMN.toString());
        System.out.println(Season.WINTER.toString());
        System.out.println(Season.SPRING.name());
        System.out.println(Season.SPRING.hashCode());
        //输出相应的编号,编号从0开始
        System.out.println(Season.SPRING.ordinal());
        Season []values=Season.values();
        for (Season A :values){
            System.out.println(A);
        }
        System.out.println(Season.valueOf("SPRING"));
        //System.out.println(Season.valueOf("dj"));
        System.out.println(Season.SPRING.compareTo(Season.AUTUMN));

    }
}
enum Season{
    SPRING("春天","温暖"),
    SUMMER ("夏天","炎热"),
    AUTUMN ("秋天","凉爽"),
    WINTER ("冬天","寒冷"),
    OTHER;
    private String season, characteristic;
    /**将构造其私有化,防止在main方法中直接new对象*/
    private Season(){}
    private Season(String season,String characteristic){
        this.season=season;
        this.characteristic=characteristic;
    }


    @Override
    public String toString(){
        return season+characteristic;
    }

}


 练习:输出每一周的星期

package Learn;

public class Exercise {
    public static void main(String[] args) {
        Week [] weeks= Week.values();
        for (Week week :weeks){
            System.out.println(week.toString());
        }

    }
}
enum Week{
    Monday("星期一"),
    Tuesday("星期二"),
    Wednesday("星期三"),
    Thursday("星期四"),
    Friday("星期五"),
    Saturday("星期六"),
    Sunday("星期日");

    private String week;
    Week(String week){
        this.week=week;
    }

    @Override
    public String toString() {
        return  week ;
    }
}

注意:

如果我们使用enum关键字之后,就不可以继承其他类了,Java是单继承机制。但是实质上enum还是一个类,所以可以继承实现接口。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值