枚举的一些知识点

枚举的一些知识点

先看一下我们自己写的枚举类,我想写的都写在代码里了

public class MyEnum {

    private String desc;

    private String name;

    //提供公开的类属性

    public static final MyEnum SPRING = new MyEnum("春","春暖花开");

    public static final MyEnum SUMMER = new MyEnum("夏","夏日炎炎");

    //把构造方法私有化无法创建新的对象

    private MyEnum() {

    }

    private MyEnum(String desc, String name) {

        this.desc = desc;

        this.name = name;

    }

    //只提供get方法,不提供set方法

    public String getByDesc(String desc) {

        return desc;

    }

    public String getByName(String name) {

        return name;

    }

    @Override

    public String toString() {

        return "MyEnum{" +

                "desc='" + desc + '\'' +

                ", name='" + name + '\'' +

                '}';

    }

}

再看看正规的枚举类

//枚举类其实继承了一个Enum的大枚举类,这个枚举类替我们做了很多事

// 在编译期间就还原出来了,这也属于java的一个语法糖

public enum Season {

    //必须第一行,其后面可以是无参和有参的,前提是类里得有,而且内置了一个索引从0开始

    SPRING("春暖花开","春"),

    SUMMER("夏日炎炎","夏");

    private String desc;

    private String name;

    //构造方法默认是私有的

    private Season() {

    }

    Season(String desc, String name) {

        this.desc = desc;

        this.name = name;

    }

    //在真正的枚举类里是能修改属性的

    public void setDesc(String desc) {

        this.desc = desc;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getDesc() {

        return desc;

    }

    public String getName() {

        return name;

    }

}

最后再看看枚举里一些方法

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

        //枚举的六个方法

        //1.name(),返回对应枚举的名字

        System.out.println("name(): " + Season.SPRING);

        //2.ordinal()返回枚举的索引,从0开始

        System.out.println("ordinal(): " + Season.SUMMER.ordinal());

        //3.values()返回一个数组,在源码中也说明了这个隐式的方法,在编译阶段生成

        /**

         * <p>Note that for a particular enum type {@code T}, the

         * implicitly declared {@code public static T valueOf(String)}

         * method on that enum may be used instead of this method to map

         * from a name to the corresponding enum constant.  All the

         * constants of an enum type can be obtained by calling the

         * implicit {@code public static T[] values()} method of that

         * type.*/

        System.out.println("values(): " + Arrays.toString(Season.values()));

        //4.valueOf(),返回对应的枚举常量,之所以打印名称是因为没有重写toString

        System.out.println("valueOf(): " + Season.valueOf("SPRING"));

        //5.toSpring,返回的其实就是名称

        System.out.println("toSpring: " + Season.SPRING);

        //6.compareTo(),返回下标相减的索引值

        System.out.println("compareTo(): " + Season.SPRING.compareTo(Season.SUMMER));

    }

}

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值