枚举类笔记

枚举

一、基础入门

1.常用范围

一般用于定义一组相类似的常量

2.古老方式定义枚举类

public class TestOne {
    public static void main(String[] args) {
        System.out.println(People.LIU);
        System.out.println(People.LI);
    }
}

/**
 * 自定义枚举类
 */
class People {
    private final String name;
    private final Integer age;

    People(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public static final People LIU = new People("liu", 12);
    public static final People LI = new People("li", 23);

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3.Enum关键字定义

3.1 默认覆盖
/**
 * @author hollow
 * 枚举类写法
 */
public class TestOne {
    public static void main(String[] args) {
        System.out.println(People.LIU);
        System.out.println(People.LI);
        //补充getClass和getDeclaringClass的区别 看getDeclaringClass源码
        System.out.println(People.LI.getDeclaringClass());
        System.out.println(People.LI.getClass());
    }
}

/**
 * 自定义枚举类
 */
enum People {
    /**
     * 人物信息
     */
    LIU("liu",12),
    LI("li",24);

    private final String name;
    private final Integer age;

    People(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }
    
}

结果图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3JzQ0tOi-1642423587372)(D:\LZ\HollowNote\三消项目\枚举类.assets\1642421649402.png)]

差异主要看源码,源码如下

    public final Class<E> getDeclaringClass() {
        Class<?> clazz = getClass();
        Class<?> zuper = clazz.getSuperclass();
        return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper;
    }

多判定更加稳定

3.2 手动覆盖
    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

结果图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bx5K16Lw-1642423587375)(D:\LZ\HollowNote\三消项目\枚举类.assets\1642421578155.png)]

二、常用方法

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fahC6EtU-1642423587376)(D:\LZ\HollowNote\三消项目\枚举类.assets\1642421732996.png)]

1.values()

/**
 * @author hollow
 * 枚举类写法
 */
public class TestOne {
    public static void main(String[] args) {
        System.out.println(People.LIU);
        System.out.println(People.LI);
        //补充getClass和getDeclaringClass的区别 看getDeclaringClass源码
        System.out.println(People.LI.getDeclaringClass());
        System.out.println(People.LI.getClass());
        System.out.println("----------------------------------------");

        for (People value : People.values()) {
            System.out.println(value);
        }
    }
}

/**
 * 自定义枚举类
 */
enum People {
    /**
     * 人物信息
     */
    LIU("liu",12),
    LI("li",24);

    private final String name;
    private final Integer age;

    People(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

}

结果图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QKqeqlPW-1642423587376)(D:\LZ\HollowNote\三消项目\枚举类.assets\1642421872003.png)]

PS:个人理解,就是通过values()将自定义枚举类变成一个数组

2.valueOf()

很简单…就是在括号内输入想要的对象名,得到该对象

        System.out.println("---------------------------------------");
        People liu = People.valueOf("LIU");
        System.out.println(liu);

结果图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FExMpTXy-1642423587377)(D:\LZ\HollowNote\三消项目\枚举类.assets\1642422354734.png)]

3.toString()

System.out.println("---------------------------------------");System.out.println(People.LI.toString());

结果图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-btWy5Dqw-1642423587377)(D:\LZ\HollowNote\三消项目\枚举类.assets\1642422505154.png)]

三、接口实现

1.接口实现

/**
 * @author hollow
 * 枚举类接口实现
 */
public class TestOne {
    public static void main(String[] args) {
        People.valueOf("LIU").show();
        People.valueOf("LI").show();
        People.LIU.show();
        People.LI.show();

        System.out.println(People.LI.getDeclaringClass());
        System.out.println(People.LI.getClass());
    }
}

/**
 * 详细展示
 */
interface DetailInfo {
    /**
     * 详细显示
     */
    void show();
}

/**
 * 自定义枚举类
 */
enum People implements DetailInfo{
    /**
     * 人物信息
     */
    LIU("liu",12){
        @Override
        public void show() {
            System.out.println("溜溜溜");
        }
    },
    LI("li",24) {
        @Override
        public void show() {
            System.out.println("莉莉力");
        }
    };

    private final String name;
    private final Integer age;

    People(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

}

2.结果图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-F0Q0QJlq-1642423587378)(D:\LZ\HollowNote\三消项目\枚举类.assets\1642423221046.png)]

这里我们就可以发现,上面我提到的getDeclaringClassgetClass的区别了。

参考:https://www.bilibili.com/video/BV1Kb411W75N?p=502

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值