3

魔功心法-枚举篇 - 天下没有收费的bug

 1 year ago
source link: https://www.cnblogs.com/LoveBB/p/17286874.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

前言:
https://www.cnblogs.com/LoveBB/p/17277662.html

什么是枚举

枚:量词。一般用于较小的片状物,相当于“个”。

举:提出:列举。举一反三。举个例子。

所以,枚举就是一个个列举出来

枚举的作用

魔功的作用,就不过多描述了,主打的就是一个优雅。

枚举的使用

枚举的成员方法

在学习枚举之前,我们很有必要了解他的几个比较重要的成员方法。我们先定义一个枚举如下:

public enum TestEnum {
}
  • 这里可以看到,枚举中有两个比较重要的方法,分别是 values() 和 valueOf();
    2138456-20230404161955789-531261408.png
方法名称 具体含义 使用方法
values() 该方法可以将枚举类型成员以数组的形式返回 枚举类型名称.values()
valueOf() 该方法可以实现将普通字符串转换为枚举实例 枚举类型名称.valueOf("ABC")
  • 下面我们随便在枚举中添加两个属性 boy 和 girl
// 规范写法
public enum TestEnum {
    BOY,GIRL
}

// 属性大写只是一种规范,小写也是可以的
public enum TestEnum {
    Boy,Girl
}

// 既然小写可以,那么乱写也是没有问题的
public enum TestEnum {
    BoY,giRL
}
  • 我们使用规范的方法,试试枚举中的 values() 方法,这个方法是拿到 TestEnum 中的所有属性,并且以数组的形式返回。
2138456-20230404162009326-1835602428.png

  • 试试枚举中的 values() 方法
2138456-20230404162025304-1141742371.png

从上面可以看到,这三者的返回值是一样的,类型都是 TestEnum。那这个方法就没啥好看的了,就是拿到 TestEnum 中的单个属性。

枚举与常量类的对比

public enum TestEnum {
    BOY,GIRL
}
public class TestConstant {
    private static Integer BOY;
    private static Integer GIRL;
}
  • 枚举和常量类的对比一目了然。但是一般情况下,我们使用常量类都是下面这种形式。
public class TestConstant {
  	// 男
    private static final Integer BOY = 0;
    // 女
    private static Integer GIRL = 1;
}
  • 这样看起来是不是更加亲切了,那么枚举类应该怎么写呢,效果如下
@Getter
public enum TestEnum {
    BOY(0),GIRL(1);
    private Integer code;
    TestEnum(Integer code){
        this.code = code;
    }

}
  • 两者使用对比
public static void main(String[] args) {
    // 使用常量类
    Integer boy = TestConstant.BOY;
    Integer girl = TestConstant.GIRL;

    // 使用枚举
    Integer enumBoy = TestEnum.BOY.getCode();
    Integer girlCode = TestEnum.GIRL.getCode();
}

枚举与常量类的使用

  • 众所周知,我们对前端展示的 性别 这个属性,一般在数据表中都是存 0,1。这样的话,每次查询和入库都需要做转换。
@Data
public class People {
   private String gender;
}
  • 常量类转化
People people = new People();
// int 转为 String
Integer boy = TestConstant.BOY;
if (boy == 0) {
    people.setGender("男");
}
Integer girl = TestConstant.GIRL;
if (girl == 1) {
    people.setGender("女");
}
People people = new People();
// 使用枚举
Integer enumBoy = TestEnum.BOY.getCode();
if (enumBoy == 0) {
    people.setGender("男");
}
Integer girlCode = TestEnum.GIRL.getCode();
if (girlCode == 1) {
    people.setGender("女");
}

毫无违和感,那还要枚举干啥勒。别着急,下面就介绍枚举的另一种用法 :

@Getter
public enum TestEnum {
    BOY(0,"男"),GIRL(1,"女");
    private Integer code;
    private String name;
    TestEnum(Integer code,String name){
        this.code = code;
    }
}
  • 枚举转化 2
People people = new People();
// 使用枚举
Integer enumBoy = TestEnum.BOY.getCode();
if (enumBoy == 0) {
    String name = TestEnum.BOY.getName();
    people.setGender(name);
}

Integer girlCode = TestEnum.GIRL.getCode();
if (girlCode == 1) {
    String name = TestEnum.GIRL.getName();
    people.setGender(name);
}

真好,如果工资是按代码行数发的,那你就能多拿两打工资了

  • 枚举转化3

上面 2 的写法,基本上都是固定了,不符合面向对象的写法,我们应该抽象出来这一个方法,方法的入参是 int 类型,返回值是 String 类型。比如传入一个 0,返回一个男,传入一个 1,返回一个女。这个时候前面的这个 values() 方法就派上用场了

@Getter
public enum TestEnum {
    BOY(0,"男"),GIRL(1,"女");
    private Integer code;
    private String name;
    TestEnum(Integer code,String name){
        this.code = code;
    }

    // 这个是抽象的方法,放在枚举里面 
    public static String getName(int code) {
        // 遍历 TestEnum.values()
        for (TestEnum testEnum : TestEnum.values()) {
            // 如果枚举 code 和传入的 code相同,返回对应的文字
            if (testEnum.getCode() == code) {
                return testEnum.name;
            }
        }
        // 不匹配,返回默认值
        return null;
    } 

}
  • 使用方法改造
People people = new People();
// 使用枚举
Integer enumBoy = TestEnum.BOY.getCode();
if (enumBoy == 0) {
    people.setGender(TestEnum.getName(0));
}

Integer girlCode = TestEnum.GIRL.getCode();
if (girlCode == 1) {
    people.setGender(TestEnum.getName(1));
}

是不是有高手那味了,这就是魔功枚举的强大之处,随随便便一个常量,就硬生生给你整成高手范。


枚举的进阶

  • 完了嘛?如果只是简单的使用,确实是完了,但是我们的追求远不止如此,我们用了枚举,就一定要用出高手风范:一切皆可枚举

三属性枚举

要获取每个网站的地址,要把每个网站的地址存储起来,网站地址都是已知的。并且不做维护。

  • 如果要建立一张表,大概如下:
2138456-20230404162050371-1211622566.png
  • 如果我们用枚举来实现,就是下面这样
@Getter
public enum InternetEnum {
    BAIDU(1,"百度","www.baidu.com"),
    HUOHU(2,"火狐","www.huohu.com"),
    GUGLE(3,"谷歌","www.guge.com"),
    SLO(4,"360","www.360.com"),
    QQ(5,"qq","www.qq.com");
    private Integer code;
    private String name;
    private String address;
    private InternetEnum(int code,String name,String address) {
    this.code = code;
    this.name = name;
    this.address = address;
    }

    private String getName(int code){
        for (InternetEnum internetEnum : InternetEnum.values()) {
            if (internetEnum.getCode()==code){
                return internetEnum.getName();
            }
        }
        return null;
    }

    private String getAddress(int code){
        for (InternetEnum internetEnum : InternetEnum.values()) {
            if (internetEnum.getCode()==code){
                return internetEnum.getAddress();
            }
        }
        return null;
    }
}

这里只是举个例子,前提是这些属性不经常变的,如果是经常变的,还是得考虑使用数据表。至于哪些是不变的,如果你维护过别人的代码,就一定能见过很多这样的东西,这时候大胆使用枚举改掉他。

  • 有了三属性,就有四属性,五属性,不知道,当你看到下面这种枚举的时候,你会不会陷入沉思,并且优雅亲切来上一句问候
2138456-20230404162106191-333573080.png

枚举神奇之处

  • 枚举用顺手之后,万物皆可枚举
  • 对前端:传 0,1 入参就行
  • 对数据表,咱就存 0,1 就行
  • 这样一来,主动权就在你手里了,当别人看着数据表一堆 0,1 陷入沉思的时候、当前端其他人来接手你的代码时,前端传给他一堆 0,1的时候,就是魔功显示威力的时候。
2138456-20230404162115214-2078898196.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK