【Java 枚举】

78 篇文章 0 订阅

自定义枚举类

package com.yuzhenc.enums;

/**
 * @author: yuzhenc
 * @date: 2022-04-13 22:17:53
 * @desc: com.yuzhenc.enums
 * @version: 1.0
 */
public class Test01 {
    public static void main(String[] args) {
        Season season = Season.AUTUMN;
        System.out.println(season);//Season{seasonName='秋天', seasonDesc='硕果累累'}
        System.out.println(season.getSeasonName());//秋天
        System.out.println(season.getSeasonDesc());//硕果累累
    }
}
class Season {
    //属性:
    private final String seasonName ;//季节名字
    private final String seasonDesc ;//季节描述
    //利用构造器对属性进行赋值操作:
    //构造器私有化,外界不能调用这个构造器,只能Season内部自己调用
    private Season(String seasonName,String seasonDesc){
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }
    //提供枚举类的有限的  确定的对象:
    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("冬天","冰天雪地");
    //额外因素:
    public String getSeasonName() {
        return seasonName;
    }
    public String getSeasonDesc() {
        return seasonDesc;
    }
    //toString();
    @Override
    public String toString() {
        return "Season{" +
                "seasonName='" + seasonName + '\'' +
                ", seasonDesc='" + seasonDesc + '\'' +
                '}';
    }
}

enum关键字

package com.yuzhenc.enums;

/**
 * @author: yuzhenc
 * @date: 2022-04-13 22:24:04
 * @desc: com.yuzhenc.enums
 * @version: 1.0
 */
public class Test02 {
    public static void main(String[] args) {
        Season1 season1 = Season1.AUTUMN;
        System.out.println(season1);//Season1{seasonName='秋天', seasonDesc='硕果累累'}
        System.out.println(season1.getSeasonName());//秋天
        System.out.println(season1.getSeasonDesc());//硕果累累
    }
}

enum Season1 {
    //提供枚举类的有限的  确定的对象:--->enum枚举类要求对象(常量)必须放在最开始位置
    //多个对象之间用,进行连接,最后一个对象后面用;结束
    SPRING("春天","春暖花开"),
    SUMMER("夏天","烈日炎炎"),
    AUTUMN("秋天","硕果累累"),
    WINTER("冬天","冰天雪地");
    //属性:
    private final String seasonName ;//季节名字
    private final String seasonDesc ;//季节描述
    //利用构造器对属性进行赋值操作:
    //构造器私有化,外界不能调用这个构造器,只能Season内部自己调用
    private Season1(String seasonName, String seasonDesc){
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }
    //额外因素:
    public String getSeasonName() {
        return seasonName;
    }
    public String getSeasonDesc() {
        return seasonDesc;
    }
    //toString();
    @Override
    public String toString() {
        return "Season1{" +
                "seasonName='" + seasonName + '\'' +
                ", seasonDesc='" + seasonDesc + '\'' +
                '}';
    }
}

常用方法

package com.yuzhenc.enums;

/**
 * @author: yuzhenc
 * @date: 2022-04-13 22:32:27
 * @desc: com.yuzhenc.enums
 * @version: 1.0
 */
public class Test03 {
    public static void main(String[] args) {
        Season1 season1 = Season1.SPRING;
        //用enum关键字创建的Season枚举类上面的父类是:java.lang.Enum,常用方法子类Season可以直接拿过来使用:
        //toString();--->获取对象的名字(若子类未重写)
        System.out.println(season1/*.toString()*/);//Season1{seasonName='春天', seasonDesc='春暖花开'}
        //values:返回枚举类对象的数组
        Season1[] season1s = Season1.values();
        for (Season1 season11:season1s) {
            System.out.println(season11);
        }
        /*
            Season1{seasonName='春天', seasonDesc='春暖花开'}
            Season1{seasonName='夏天', seasonDesc='烈日炎炎'}
            Season1{seasonName='秋天', seasonDesc='硕果累累'}
            Season1{seasonName='冬天', seasonDesc='冰天雪地'}
        */
        //valueOf:通过对象名字获取这个枚举对象
        Season1 season11 = Season1.valueOf("SPRING");
        System.out.println(season11);//Season1{seasonName='春天', seasonDesc='春暖花开'}
    }
}

枚举类实现接口

package com.yuzhenc.enums;

/**
 * @author: yuzhenc
 * @date: 2022-04-13 22:39:06
 * @desc: com.yuzhenc.enums
 * @version: 1.0
 */
public class Test04 {
    public static void main(String[] args) {
        //所有枚举对象调用的都是同一个方法
        Season2 spring = Season2.SPRING;
        spring.show();//这是Season2....
        Season2 summer = Season2.SUMMER;
        summer.show();//这是Season2....
    }
}
interface TestInterface {
    void show();
}

enum Season2 implements TestInterface{
    SPRING,
    SUMMER,
    AUTUMN,
    WINTER;
    @Override
    public void show(){
        System.out.println("这是Season2....");
    }
}
package com.yuzhenc.enums;

/**
 * @author: yuzhenc
 * @date: 2022-04-13 22:43:59
 * @desc: com.yuzhenc.enums
 * @version: 1.0
 */
public class Test05 {
    public static void main(String[] args) {
        //不同的对象  调用的show方法也不同
        Season3 spring = Season3.SPRING;
        spring.show();//这是春天
        Season3 summer = Season3.SUMMER;
        summer.show();//这是夏天
    }
}
enum Season3 implements TestInterface {
    SPRING{
        @Override
        public void show() {
            System.out.println("这是春天");
        }
    },
    SUMMER{
        @Override
        public void show() {
            System.out.println("这是夏天");
        }
    },
    AUTUMN{
        @Override
        public void show() {
            System.out.println("这是秋天");
        }
    },
    WINTER{
        @Override
        public void show() {
            System.out.println("这是冬天");
        }
    };

}

枚举结合switch

package com.yuzhenc.enums;

/**
 * @author: yuzhenc
 * @date: 2022-04-13 22:48:17
 * @desc: com.yuzhenc.enums
 * @version: 1.0
 */
public class Test06 {
    public static void main(String[] args) {
        Gender sex = Gender.;
        //switch后面的()中可以传入枚举类型
        //switch后面的():int,short,byte,char,String ,枚举
        switch (sex){
            case:
                System.out.println("是个女孩");
                break;
            case:
                System.out.println("是个男孩");
                break;
        }
    }
}
enum Gender {,;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sqlboy-yuzhenc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值