博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
拔高你的Java代码质量吧:推荐使用枚举定义常量(转)
阅读量:7110 次
发布时间:2019-06-28

本文共 3205 字,大约阅读时间需要 10 分钟。

提高你的Java代码质量吧:推荐使用枚举定义常量

一、分析 

常量的声明是每一个项目中不可或缺的,在Java1.5之前,我们只有两种方式的声明:类常量和接口常量。不过,在1.5版之后有了改进,即新增了一种常量声明方式,枚举常量。代码如下: 

 

enum Season{     Spring,Summer,Autumn,Winter; }

 

二、场景 

那么枚举常量与我们的经常使用的类常量和静态常量比有什么优势呢 

1.枚举常量更简单 

先把Season枚举翻译成接口,代码如下 

 

interface Season{     int Sprint = 0;     int Summer = 1;     int Autumn = 2;     int Winter = 3; }

 

枚举只需要定义每个枚举项,不需要定义枚举值,而接口常量(或类常量)则必须定义值,否则编译通不过;两个引用的方式相同(都是“类名.属性”,如Season.Sprint),但是枚举表示的是一个枚举项,字面含义是春天,而接口常量却是一个Int类型 

2.枚举常量属于稳态型 

使用常量接口,我们得对输入值进行检查,确定是否越界,如果常量非常庞大,校验输入就是一件非常麻烦的事情,但这是一个不可逃避的过程。 

 

public void describe(int s){     //s变量不能超越边界,校验条件     if(s >= 0 && s <4){         switch(s){             case Season.Summer:                 System.out.println("Summer is very hot!");                 break;             case Season.Winter:                 System.out.println("Winter is very cold!");                 break;         …..         }     } }

 

我们再来看看枚举常量是否能够避免校验问题,代码如下 

 

public void describe(Season s){     switch(s){         case Season.Summer:             System.out.println("Summer is very hot!");             break;         case Season.Winter:             System.out.println("Winter is very cold!");             break;         …...     } }

 

不用校验,已经限定了是Season枚举,所以只能是Season类的四个实例。这也是我们看重枚举的地方:在编译期间限定类型,不允许发生越界的情况。 

3.枚举具有内置方法 

每个枚举都是java.lang.Enum的子类,该基类提供了诸如获得排序值的ordinal方法、compareTo比较方法等,大大简化了常量的访问。比如,列出所有枚举值: 

 

public static void main(String[] args){     for(Season s:Season.values()){         System.out.println(s);     } }

 

4.枚举可以自定义方法 

这一点似乎不是枚举的优点,类常量也可以有自己的方法,但关键是枚举常量不仅仅可以定义静态方法,还可以定义非静态方法,而且还能够从根本上杜绝常量类被实例化。比如我们在定义获取最舒服的季节,使用枚举的代码如下: 

 

enum Season{     Spring,Summer,Autumn,Winter;     //最舒服的季节     public static Season getComfortableSeason(){         return Spring;     } }

 

那如果是使用类常量如何实现呢?如下 

 

class Season{     public final static int Spring = 0;     public final static int Summer = 1;     public final static int Autumn = 2;     public final static int Winter = 3;      //最舒服的季节     public static int getComfortableSeason(){         return Spring;     } }

 

虽然枚举在很多方面都比接口常量和类常量好用,但是它有一点比不上接口常量和类常量的,就是继承,枚举类型是不能有继承的,也就是说一个枚举常量定义完毕后,除非修改重构,否则无法做扩展。 

三、建议 

在项目开发中,推荐使用枚举常量代替接口常量或类常量 

 

Java: `enum` vs `String` as Parameters:

I would consider Enums to be a better approach than Strings. They are type safe and comparing them is faster than comparing Strings.

As a pre Java 1.5 alternative you could use the type-safe enum pattern suggested by Joshua Bloch in his book Effective Java. For type-safe enums see also

 

If your set of parameters is limited and known at compile time, use Enum.

If your set of parameters is open and unkown at compile time, use strings.

Just because you declare a public final String as something you expect to have passed into a method as a parameter, there's nothing stopping me from passing in anything I like.

Using enums means that I can't create my own object to pass in, protecting both sides of the issue. The only time I can think you should be using constant Strings in lieu of enums is if you need to allow room for the user to extend your method to enable custom functionality...

http://stackoverflow.com/questions/277364/java-enum-vs-string-as-parameters/5748871#5748871

 

转载于:https://www.cnblogs.com/softidea/p/3821796.html

你可能感兴趣的文章
Branckets快捷键
查看>>
windows下nodejs的安装和hello world小应用的创建
查看>>
赶紧登陆百家号看看,有没有被降级
查看>>
使用Eclipse进行PHP的服务器端调试
查看>>
在Visual Sutdio 2017中使用boost库
查看>>
Java中的‘锁’- synchronized、ReentrantLock、ReentrantReadWriteLock
查看>>
让网站成为 HTTPS 安全站点
查看>>
shell编程——如何实现命令行选项的各种个性功能
查看>>
不懂代码的,但是这些都看懂了。程序员段子合集
查看>>
ATAC-Seq 数据分析(上)
查看>>
Confluence 6 从 Crowd 或 JIRA 应用中切换回使用内部用户管理
查看>>
Python全栈 Web(jQuery 一条龙服务)
查看>>
每日文献:2018-01-29
查看>>
Stack Overflow 那些让人头大的规矩
查看>>
Python网易云音乐爬虫进阶篇
查看>>
WPS Office 2019 上架微软商城,全新可定制 UI
查看>>
高性能网络IO模型
查看>>
REST表述性状态传递
查看>>
服务器宕机原因
查看>>
Spring Cloud之极端续租间隔时间的影响
查看>>